Make cp_remove_params return a gdb::unique_xmalloc_ptr
[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             && SYMBOL_MATCHES_SEARCH_NAME (sym, name))
533           return stab;
534         if (with_opaque != NULL
535             && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, name))
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 static gdb::unique_xmalloc_ptr<char>
629 psymtab_search_name (const char *name)
630 {
631   switch (current_language->la_language)
632     {
633     case language_cplus:
634       {
635         if (strchr (name, '('))
636           {
637             gdb::unique_xmalloc_ptr<char> ret = cp_remove_params (name);
638
639             if (ret)
640               return ret;
641           }
642       }
643       break;
644
645     default:
646       break;
647     }
648
649   return gdb::unique_xmalloc_ptr<char> (xstrdup (name));
650 }
651
652 /* Look, in partial_symtab PST, for symbol whose natural name is NAME.
653    Check the global symbols if GLOBAL, the static symbols if not.  */
654
655 static struct partial_symbol *
656 lookup_partial_symbol (struct objfile *objfile,
657                        struct partial_symtab *pst, const char *name,
658                        int global, domain_enum domain)
659 {
660   struct partial_symbol **start, **psym;
661   struct partial_symbol **top, **real_top, **bottom, **center;
662   int length = (global ? pst->n_global_syms : pst->n_static_syms);
663   int do_linear_search = 1;
664
665   if (length == 0)
666     return NULL;
667
668   gdb::unique_xmalloc_ptr<char> search_name = psymtab_search_name (name);
669   start = (global ?
670            objfile->global_psymbols.list + pst->globals_offset :
671            objfile->static_psymbols.list + pst->statics_offset);
672
673   if (global)                   /* This means we can use a binary search.  */
674     {
675       do_linear_search = 0;
676
677       /* Binary search.  This search is guaranteed to end with center
678          pointing at the earliest partial symbol whose name might be
679          correct.  At that point *all* partial symbols with an
680          appropriate name will be checked against the correct
681          domain.  */
682
683       bottom = start;
684       top = start + length - 1;
685       real_top = top;
686       while (top > bottom)
687         {
688           center = bottom + (top - bottom) / 2;
689           if (!(center < top))
690             internal_error (__FILE__, __LINE__,
691                             _("failed internal consistency check"));
692           if (strcmp_iw_ordered (SYMBOL_SEARCH_NAME (*center),
693                                  search_name.get ()) >= 0)
694             {
695               top = center;
696             }
697           else
698             {
699               bottom = center + 1;
700             }
701         }
702       if (!(top == bottom))
703         internal_error (__FILE__, __LINE__,
704                         _("failed internal consistency check"));
705
706       /* For `case_sensitivity == case_sensitive_off' strcmp_iw_ordered will
707          search more exactly than what matches SYMBOL_MATCHES_SEARCH_NAME.  */
708       while (top >= start && SYMBOL_MATCHES_SEARCH_NAME (*top,
709                                                          search_name.get ()))
710         top--;
711
712       /* Fixup to have a symbol which matches SYMBOL_MATCHES_SEARCH_NAME.  */
713       top++;
714
715       while (top <= real_top
716              && SYMBOL_MATCHES_SEARCH_NAME (*top, search_name.get ()))
717         {
718           if (symbol_matches_domain (SYMBOL_LANGUAGE (*top),
719                                      SYMBOL_DOMAIN (*top), domain))
720             return *top;
721           top++;
722         }
723     }
724
725   /* Can't use a binary search or else we found during the binary search that
726      we should also do a linear search.  */
727
728   if (do_linear_search)
729     {
730       for (psym = start; psym < start + length; psym++)
731         {
732           if (symbol_matches_domain (SYMBOL_LANGUAGE (*psym),
733                                      SYMBOL_DOMAIN (*psym), domain)
734               && SYMBOL_MATCHES_SEARCH_NAME (*psym, search_name.get ()))
735             return *psym;
736         }
737     }
738
739   return NULL;
740 }
741
742 /* Get the symbol table that corresponds to a partial_symtab.
743    This is fast after the first time you do it.
744    The result will be NULL if the primary symtab has no symbols,
745    which can happen.  Otherwise the result is the primary symtab
746    that contains PST.  */
747
748 static struct compunit_symtab *
749 psymtab_to_symtab (struct objfile *objfile, struct partial_symtab *pst)
750 {
751   /* If it is a shared psymtab, find an unshared psymtab that includes
752      it.  Any such psymtab will do.  */
753   while (pst->user != NULL)
754     pst = pst->user;
755
756   /* If it's been looked up before, return it.  */
757   if (pst->compunit_symtab)
758     return pst->compunit_symtab;
759
760   /* If it has not yet been read in, read it.  */
761   if (!pst->readin)
762     {
763       scoped_restore decrementer = increment_reading_symtab ();
764
765       (*pst->read_symtab) (pst, objfile);
766     }
767
768   return pst->compunit_symtab;
769 }
770
771 /* Psymtab version of relocate.  See its definition in
772    the definition of quick_symbol_functions in symfile.h.  */
773
774 static void
775 psym_relocate (struct objfile *objfile,
776                const struct section_offsets *new_offsets,
777                const struct section_offsets *delta)
778 {
779   struct partial_symbol **psym;
780   struct partial_symtab *p;
781
782   ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, p)
783     {
784       p->textlow += ANOFFSET (delta, SECT_OFF_TEXT (objfile));
785       p->texthigh += ANOFFSET (delta, SECT_OFF_TEXT (objfile));
786     }
787
788   for (psym = objfile->global_psymbols.list;
789        psym < objfile->global_psymbols.next;
790        psym++)
791     {
792       fixup_psymbol_section (*psym, objfile);
793       if (SYMBOL_SECTION (*psym) >= 0)
794         SYMBOL_VALUE_ADDRESS (*psym) += ANOFFSET (delta,
795                                                   SYMBOL_SECTION (*psym));
796     }
797   for (psym = objfile->static_psymbols.list;
798        psym < objfile->static_psymbols.next;
799        psym++)
800     {
801       fixup_psymbol_section (*psym, objfile);
802       if (SYMBOL_SECTION (*psym) >= 0)
803         SYMBOL_VALUE_ADDRESS (*psym) += ANOFFSET (delta,
804                                                   SYMBOL_SECTION (*psym));
805     }
806 }
807
808 /* Psymtab version of find_last_source_symtab.  See its definition in
809    the definition of quick_symbol_functions in symfile.h.  */
810
811 static struct symtab *
812 psym_find_last_source_symtab (struct objfile *ofp)
813 {
814   struct partial_symtab *ps;
815   struct partial_symtab *cs_pst = NULL;
816
817   ALL_OBJFILE_PSYMTABS_REQUIRED (ofp, ps)
818     {
819       const char *name = ps->filename;
820       int len = strlen (name);
821
822       if (!(len > 2 && (strcmp (&name[len - 2], ".h") == 0
823                         || strcmp (name, "<<C++-namespaces>>") == 0)))
824         cs_pst = ps;
825     }
826
827   if (cs_pst)
828     {
829       if (cs_pst->readin)
830         {
831           internal_error (__FILE__, __LINE__,
832                           _("select_source_symtab: "
833                           "readin pst found and no symtabs."));
834         }
835       else
836         {
837           struct compunit_symtab *cust = psymtab_to_symtab (ofp, cs_pst);
838
839           if (cust == NULL)
840             return NULL;
841           return compunit_primary_filetab (cust);
842         }
843     }
844   return NULL;
845 }
846
847 /* Psymtab version of forget_cached_source_info.  See its definition in
848    the definition of quick_symbol_functions in symfile.h.  */
849
850 static void
851 psym_forget_cached_source_info (struct objfile *objfile)
852 {
853   struct partial_symtab *pst;
854
855   ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, pst)
856     {
857       if (pst->fullname != NULL)
858         {
859           xfree (pst->fullname);
860           pst->fullname = NULL;
861         }
862     }
863 }
864
865 static void
866 print_partial_symbols (struct gdbarch *gdbarch,
867                        struct partial_symbol **p, int count, const char *what,
868                        struct ui_file *outfile)
869 {
870   fprintf_filtered (outfile, "  %s partial symbols:\n", what);
871   while (count-- > 0)
872     {
873       QUIT;
874       fprintf_filtered (outfile, "    `%s'", SYMBOL_LINKAGE_NAME (*p));
875       if (SYMBOL_DEMANGLED_NAME (*p) != NULL)
876         {
877           fprintf_filtered (outfile, "  `%s'", SYMBOL_DEMANGLED_NAME (*p));
878         }
879       fputs_filtered (", ", outfile);
880       switch (SYMBOL_DOMAIN (*p))
881         {
882         case UNDEF_DOMAIN:
883           fputs_filtered ("undefined domain, ", outfile);
884           break;
885         case VAR_DOMAIN:
886           /* This is the usual thing -- don't print it.  */
887           break;
888         case STRUCT_DOMAIN:
889           fputs_filtered ("struct domain, ", outfile);
890           break;
891         case LABEL_DOMAIN:
892           fputs_filtered ("label domain, ", outfile);
893           break;
894         default:
895           fputs_filtered ("<invalid domain>, ", outfile);
896           break;
897         }
898       switch (PSYMBOL_CLASS (*p))
899         {
900         case LOC_UNDEF:
901           fputs_filtered ("undefined", outfile);
902           break;
903         case LOC_CONST:
904           fputs_filtered ("constant int", outfile);
905           break;
906         case LOC_STATIC:
907           fputs_filtered ("static", outfile);
908           break;
909         case LOC_REGISTER:
910           fputs_filtered ("register", outfile);
911           break;
912         case LOC_ARG:
913           fputs_filtered ("pass by value", outfile);
914           break;
915         case LOC_REF_ARG:
916           fputs_filtered ("pass by reference", outfile);
917           break;
918         case LOC_REGPARM_ADDR:
919           fputs_filtered ("register address parameter", outfile);
920           break;
921         case LOC_LOCAL:
922           fputs_filtered ("stack parameter", outfile);
923           break;
924         case LOC_TYPEDEF:
925           fputs_filtered ("type", outfile);
926           break;
927         case LOC_LABEL:
928           fputs_filtered ("label", outfile);
929           break;
930         case LOC_BLOCK:
931           fputs_filtered ("function", outfile);
932           break;
933         case LOC_CONST_BYTES:
934           fputs_filtered ("constant bytes", outfile);
935           break;
936         case LOC_UNRESOLVED:
937           fputs_filtered ("unresolved", outfile);
938           break;
939         case LOC_OPTIMIZED_OUT:
940           fputs_filtered ("optimized out", outfile);
941           break;
942         case LOC_COMPUTED:
943           fputs_filtered ("computed at runtime", outfile);
944           break;
945         default:
946           fputs_filtered ("<invalid location>", outfile);
947           break;
948         }
949       fputs_filtered (", ", outfile);
950       fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (*p)), outfile);
951       fprintf_filtered (outfile, "\n");
952       p++;
953     }
954 }
955
956 static void
957 dump_psymtab (struct objfile *objfile, struct partial_symtab *psymtab,
958               struct ui_file *outfile)
959 {
960   struct gdbarch *gdbarch = get_objfile_arch (objfile);
961   int i;
962
963   if (psymtab->anonymous)
964     {
965       fprintf_filtered (outfile, "\nAnonymous partial symtab (%s) ",
966                         psymtab->filename);
967     }
968   else
969     {
970       fprintf_filtered (outfile, "\nPartial symtab for source file %s ",
971                         psymtab->filename);
972     }
973   fprintf_filtered (outfile, "(object ");
974   gdb_print_host_address (psymtab, outfile);
975   fprintf_filtered (outfile, ")\n\n");
976   fprintf_unfiltered (outfile, "  Read from object file %s (",
977                       objfile_name (objfile));
978   gdb_print_host_address (objfile, outfile);
979   fprintf_unfiltered (outfile, ")\n");
980
981   if (psymtab->readin)
982     {
983       fprintf_filtered (outfile,
984                         "  Full symtab was read (at ");
985       gdb_print_host_address (psymtab->compunit_symtab, outfile);
986       fprintf_filtered (outfile, " by function at ");
987       gdb_print_host_address (psymtab->read_symtab, outfile);
988       fprintf_filtered (outfile, ")\n");
989     }
990
991   fprintf_filtered (outfile, "  Symbols cover text addresses ");
992   fputs_filtered (paddress (gdbarch, psymtab->textlow), outfile);
993   fprintf_filtered (outfile, "-");
994   fputs_filtered (paddress (gdbarch, psymtab->texthigh), outfile);
995   fprintf_filtered (outfile, "\n");
996   fprintf_filtered (outfile, "  Address map supported - %s.\n",
997                     psymtab->psymtabs_addrmap_supported ? "yes" : "no");
998   fprintf_filtered (outfile, "  Depends on %d other partial symtabs.\n",
999                     psymtab->number_of_dependencies);
1000   for (i = 0; i < psymtab->number_of_dependencies; i++)
1001     {
1002       fprintf_filtered (outfile, "    %d ", i);
1003       gdb_print_host_address (psymtab->dependencies[i], outfile);
1004       fprintf_filtered (outfile, " %s\n",
1005                         psymtab->dependencies[i]->filename);
1006     }
1007   if (psymtab->user != NULL)
1008     {
1009       fprintf_filtered (outfile, "  Shared partial symtab with user ");
1010       gdb_print_host_address (psymtab->user, outfile);
1011       fprintf_filtered (outfile, "\n");
1012     }
1013   if (psymtab->n_global_syms > 0)
1014     {
1015       print_partial_symbols (gdbarch,
1016                              objfile->global_psymbols.list
1017                              + psymtab->globals_offset,
1018                              psymtab->n_global_syms, "Global", outfile);
1019     }
1020   if (psymtab->n_static_syms > 0)
1021     {
1022       print_partial_symbols (gdbarch,
1023                              objfile->static_psymbols.list
1024                              + psymtab->statics_offset,
1025                              psymtab->n_static_syms, "Static", outfile);
1026     }
1027   fprintf_filtered (outfile, "\n");
1028 }
1029
1030 /* Psymtab version of print_stats.  See its definition in
1031    the definition of quick_symbol_functions in symfile.h.  */
1032
1033 static void
1034 psym_print_stats (struct objfile *objfile)
1035 {
1036   int i;
1037   struct partial_symtab *ps;
1038
1039   i = 0;
1040   ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
1041     {
1042       if (ps->readin == 0)
1043         i++;
1044     }
1045   printf_filtered (_("  Number of psym tables (not yet expanded): %d\n"), i);
1046 }
1047
1048 /* Psymtab version of dump.  See its definition in
1049    the definition of quick_symbol_functions in symfile.h.  */
1050
1051 static void
1052 psym_dump (struct objfile *objfile)
1053 {
1054   struct partial_symtab *psymtab;
1055
1056   if (objfile->psymtabs)
1057     {
1058       printf_filtered ("Psymtabs:\n");
1059       for (psymtab = objfile->psymtabs;
1060            psymtab != NULL;
1061            psymtab = psymtab->next)
1062         {
1063           printf_filtered ("%s at ",
1064                            psymtab->filename);
1065           gdb_print_host_address (psymtab, gdb_stdout);
1066           printf_filtered (", ");
1067           wrap_here ("  ");
1068         }
1069       printf_filtered ("\n\n");
1070     }
1071 }
1072
1073 /* Psymtab version of expand_symtabs_for_function.  See its definition in
1074    the definition of quick_symbol_functions in symfile.h.  */
1075
1076 static void
1077 psym_expand_symtabs_for_function (struct objfile *objfile,
1078                                   const char *func_name)
1079 {
1080   struct partial_symtab *ps;
1081
1082   ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
1083   {
1084     if (ps->readin)
1085       continue;
1086
1087     if ((lookup_partial_symbol (objfile, ps, func_name, 1, VAR_DOMAIN)
1088          != NULL)
1089         || (lookup_partial_symbol (objfile, ps, func_name, 0, VAR_DOMAIN)
1090             != NULL))
1091       psymtab_to_symtab (objfile, ps);
1092   }
1093 }
1094
1095 /* Psymtab version of expand_all_symtabs.  See its definition in
1096    the definition of quick_symbol_functions in symfile.h.  */
1097
1098 static void
1099 psym_expand_all_symtabs (struct objfile *objfile)
1100 {
1101   struct partial_symtab *psymtab;
1102
1103   ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, psymtab)
1104     {
1105       psymtab_to_symtab (objfile, psymtab);
1106     }
1107 }
1108
1109 /* Psymtab version of expand_symtabs_with_fullname.  See its definition in
1110    the definition of quick_symbol_functions in symfile.h.  */
1111
1112 static void
1113 psym_expand_symtabs_with_fullname (struct objfile *objfile,
1114                                    const char *fullname)
1115 {
1116   struct partial_symtab *p;
1117
1118   ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, p)
1119     {
1120       /* Anonymous psymtabs don't have a name of a source file.  */
1121       if (p->anonymous)
1122         continue;
1123
1124       /* psymtab_to_fullname tries to open the file which is slow.
1125          Don't call it if we know the basenames don't match.  */
1126       if ((basenames_may_differ
1127            || filename_cmp (lbasename (fullname), lbasename (p->filename)) == 0)
1128           && filename_cmp (fullname, psymtab_to_fullname (p)) == 0)
1129         psymtab_to_symtab (objfile, p);
1130     }
1131 }
1132
1133 /* Psymtab version of map_symbol_filenames.  See its definition in
1134    the definition of quick_symbol_functions in symfile.h.  */
1135
1136 static void
1137 psym_map_symbol_filenames (struct objfile *objfile,
1138                            symbol_filename_ftype *fun, void *data,
1139                            int need_fullname)
1140 {
1141   struct partial_symtab *ps;
1142
1143   ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
1144     {
1145       const char *fullname;
1146
1147       if (ps->readin)
1148         continue;
1149
1150       /* We can skip shared psymtabs here, because any file name will be
1151          attached to the unshared psymtab.  */
1152       if (ps->user != NULL)
1153         continue;
1154
1155       /* Anonymous psymtabs don't have a file name.  */
1156       if (ps->anonymous)
1157         continue;
1158
1159       QUIT;
1160       if (need_fullname)
1161         fullname = psymtab_to_fullname (ps);
1162       else
1163         fullname = NULL;
1164       (*fun) (ps->filename, fullname, data);
1165     }
1166 }
1167
1168 /* Finds the fullname that a partial_symtab represents.
1169
1170    If this functions finds the fullname, it will save it in ps->fullname
1171    and it will also return the value.
1172
1173    If this function fails to find the file that this partial_symtab represents,
1174    NULL will be returned and ps->fullname will be set to NULL.  */
1175
1176 static const char *
1177 psymtab_to_fullname (struct partial_symtab *ps)
1178 {
1179   gdb_assert (!ps->anonymous);
1180
1181   /* Use cached copy if we have it.
1182      We rely on forget_cached_source_info being called appropriately
1183      to handle cases like the file being moved.  */
1184   if (ps->fullname == NULL)
1185     {
1186       int fd = find_and_open_source (ps->filename, ps->dirname, &ps->fullname);
1187
1188       if (fd >= 0)
1189         close (fd);
1190       else
1191         {
1192           gdb::unique_xmalloc_ptr<char> fullname;
1193
1194           /* rewrite_source_path would be applied by find_and_open_source, we
1195              should report the pathname where GDB tried to find the file.  */
1196
1197           if (ps->dirname == NULL || IS_ABSOLUTE_PATH (ps->filename))
1198             fullname.reset (xstrdup (ps->filename));
1199           else
1200             fullname.reset (concat (ps->dirname, SLASH_STRING,
1201                                     ps->filename, (char *) NULL));
1202
1203           ps->fullname = rewrite_source_path (fullname.get ()).release ();
1204           if (ps->fullname == NULL)
1205             ps->fullname = fullname.release ();
1206         }
1207     }
1208
1209   return ps->fullname;
1210 }
1211
1212 /* For all symbols, s, in BLOCK that are in DOMAIN and match NAME
1213    according to the function MATCH, call CALLBACK(BLOCK, s, DATA).
1214    BLOCK is assumed to come from OBJFILE.  Returns 1 iff CALLBACK
1215    ever returns non-zero, and otherwise returns 0.  */
1216
1217 static int
1218 map_block (const char *name, domain_enum domain, struct objfile *objfile,
1219            struct block *block,
1220            int (*callback) (struct block *, struct symbol *, void *),
1221            void *data, symbol_compare_ftype *match)
1222 {
1223   struct block_iterator iter;
1224   struct symbol *sym;
1225
1226   for (sym = block_iter_match_first (block, name, match, &iter);
1227        sym != NULL; sym = block_iter_match_next (name, match, &iter))
1228     {
1229       if (symbol_matches_domain (SYMBOL_LANGUAGE (sym),
1230                                  SYMBOL_DOMAIN (sym), domain))
1231         {
1232           if (callback (block, sym, data))
1233             return 1;
1234         }
1235     }
1236
1237   return 0;
1238 }
1239
1240 /* Psymtab version of map_matching_symbols.  See its definition in
1241    the definition of quick_symbol_functions in symfile.h.  */
1242
1243 static void
1244 psym_map_matching_symbols (struct objfile *objfile,
1245                            const char *name, domain_enum domain,
1246                            int global,
1247                            int (*callback) (struct block *,
1248                                             struct symbol *, void *),
1249                            void *data,
1250                            symbol_compare_ftype *match,
1251                            symbol_compare_ftype *ordered_compare)
1252 {
1253   const int block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
1254   struct partial_symtab *ps;
1255
1256   ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
1257     {
1258       QUIT;
1259       if (ps->readin
1260           || match_partial_symbol (objfile, ps, global, name, domain, match,
1261                                    ordered_compare))
1262         {
1263           struct compunit_symtab *cust = psymtab_to_symtab (objfile, ps);
1264           struct block *block;
1265
1266           if (cust == NULL)
1267             continue;
1268           block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
1269           if (map_block (name, domain, objfile, block,
1270                          callback, data, match))
1271             return;
1272           if (callback (block, NULL, data))
1273             return;
1274         }
1275     }
1276 }
1277
1278 /* A helper for psym_expand_symtabs_matching that handles searching
1279    included psymtabs.  This returns true if a symbol is found, and
1280    false otherwise.  It also updates the 'searched_flag' on the
1281    various psymtabs that it searches.  */
1282
1283 static bool
1284 recursively_search_psymtabs
1285   (struct partial_symtab *ps, struct objfile *objfile, enum search_domain kind,
1286    gdb::function_view<expand_symtabs_symbol_matcher_ftype> sym_matcher)
1287 {
1288   struct partial_symbol **psym;
1289   struct partial_symbol **bound, **gbound, **sbound;
1290   int keep_going = 1;
1291   enum psymtab_search_status result = PST_SEARCHED_AND_NOT_FOUND;
1292   int i;
1293
1294   if (ps->searched_flag != PST_NOT_SEARCHED)
1295     return ps->searched_flag == PST_SEARCHED_AND_FOUND;
1296
1297   /* Recurse into shared psymtabs first, because they may have already
1298      been searched, and this could save some time.  */
1299   for (i = 0; i < ps->number_of_dependencies; ++i)
1300     {
1301       int r;
1302
1303       /* Skip non-shared dependencies, these are handled elsewhere.  */
1304       if (ps->dependencies[i]->user == NULL)
1305         continue;
1306
1307       r = recursively_search_psymtabs (ps->dependencies[i],
1308                                        objfile, kind, sym_matcher);
1309       if (r != 0)
1310         {
1311           ps->searched_flag = PST_SEARCHED_AND_FOUND;
1312           return true;
1313         }
1314     }
1315
1316   gbound = (objfile->global_psymbols.list
1317             + ps->globals_offset + ps->n_global_syms);
1318   sbound = (objfile->static_psymbols.list
1319             + ps->statics_offset + ps->n_static_syms);
1320   bound = gbound;
1321
1322   /* Go through all of the symbols stored in a partial
1323      symtab in one loop.  */
1324   psym = objfile->global_psymbols.list + ps->globals_offset;
1325   while (keep_going)
1326     {
1327       if (psym >= bound)
1328         {
1329           if (bound == gbound && ps->n_static_syms != 0)
1330             {
1331               psym = objfile->static_psymbols.list + ps->statics_offset;
1332               bound = sbound;
1333             }
1334           else
1335             keep_going = 0;
1336           continue;
1337         }
1338       else
1339         {
1340           QUIT;
1341
1342           if ((kind == ALL_DOMAIN
1343                || (kind == VARIABLES_DOMAIN
1344                    && PSYMBOL_CLASS (*psym) != LOC_TYPEDEF
1345                    && PSYMBOL_CLASS (*psym) != LOC_BLOCK)
1346                || (kind == FUNCTIONS_DOMAIN
1347                    && PSYMBOL_CLASS (*psym) == LOC_BLOCK)
1348                || (kind == TYPES_DOMAIN
1349                    && PSYMBOL_CLASS (*psym) == LOC_TYPEDEF))
1350               && sym_matcher (SYMBOL_SEARCH_NAME (*psym)))
1351             {
1352               /* Found a match, so notify our caller.  */
1353               result = PST_SEARCHED_AND_FOUND;
1354               keep_going = 0;
1355             }
1356         }
1357       psym++;
1358     }
1359
1360   ps->searched_flag = result;
1361   return result == PST_SEARCHED_AND_FOUND;
1362 }
1363
1364 /* Psymtab version of expand_symtabs_matching.  See its definition in
1365    the definition of quick_symbol_functions in symfile.h.  */
1366
1367 static void
1368 psym_expand_symtabs_matching
1369   (struct objfile *objfile,
1370    gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
1371    gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
1372    gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
1373    enum search_domain kind)
1374 {
1375   struct partial_symtab *ps;
1376
1377   /* Clear the search flags.  */
1378   ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
1379     {
1380       ps->searched_flag = PST_NOT_SEARCHED;
1381     }
1382
1383   ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
1384     {
1385       QUIT;
1386
1387       if (ps->readin)
1388         continue;
1389
1390       /* We skip shared psymtabs because file-matching doesn't apply
1391          to them; but we search them later in the loop.  */
1392       if (ps->user != NULL)
1393         continue;
1394
1395       if (file_matcher)
1396         {
1397           bool match;
1398
1399           if (ps->anonymous)
1400             continue;
1401
1402           match = file_matcher (ps->filename, false);
1403           if (!match)
1404             {
1405               /* Before we invoke realpath, which can get expensive when many
1406                  files are involved, do a quick comparison of the basenames.  */
1407               if (basenames_may_differ
1408                   || file_matcher (lbasename (ps->filename), true))
1409                 match = file_matcher (psymtab_to_fullname (ps), false);
1410             }
1411           if (!match)
1412             continue;
1413         }
1414
1415       if (recursively_search_psymtabs (ps, objfile, kind, symbol_matcher))
1416         {
1417           struct compunit_symtab *symtab =
1418             psymtab_to_symtab (objfile, ps);
1419
1420           if (expansion_notify != NULL)
1421             expansion_notify (symtab);
1422         }
1423     }
1424 }
1425
1426 /* Psymtab version of has_symbols.  See its definition in
1427    the definition of quick_symbol_functions in symfile.h.  */
1428
1429 static int
1430 psym_has_symbols (struct objfile *objfile)
1431 {
1432   return objfile->psymtabs != NULL;
1433 }
1434
1435 const struct quick_symbol_functions psym_functions =
1436 {
1437   psym_has_symbols,
1438   psym_find_last_source_symtab,
1439   psym_forget_cached_source_info,
1440   psym_map_symtabs_matching_filename,
1441   psym_lookup_symbol,
1442   psym_print_stats,
1443   psym_dump,
1444   psym_relocate,
1445   psym_expand_symtabs_for_function,
1446   psym_expand_all_symtabs,
1447   psym_expand_symtabs_with_fullname,
1448   psym_map_matching_symbols,
1449   psym_expand_symtabs_matching,
1450   psym_find_pc_sect_compunit_symtab,
1451   psym_map_symbol_filenames
1452 };
1453
1454 \f
1455
1456 /* This compares two partial symbols by names, using strcmp_iw_ordered
1457    for the comparison.  */
1458
1459 static int
1460 compare_psymbols (const void *s1p, const void *s2p)
1461 {
1462   struct partial_symbol *const *s1 = (struct partial_symbol * const*) s1p;
1463   struct partial_symbol *const *s2 = (struct partial_symbol * const*) s2p;
1464
1465   return strcmp_iw_ordered (SYMBOL_SEARCH_NAME (*s1),
1466                             SYMBOL_SEARCH_NAME (*s2));
1467 }
1468
1469 static void
1470 sort_pst_symbols (struct objfile *objfile, struct partial_symtab *pst)
1471 {
1472   /* Sort the global list; don't sort the static list.  */
1473
1474   qsort (objfile->global_psymbols.list + pst->globals_offset,
1475          pst->n_global_syms, sizeof (struct partial_symbol *),
1476          compare_psymbols);
1477 }
1478
1479 /* Allocate and partially fill a partial symtab.  It will be
1480    completely filled at the end of the symbol list.
1481
1482    FILENAME is the name of the symbol-file we are reading from.  */
1483
1484 struct partial_symtab *
1485 start_psymtab_common (struct objfile *objfile,
1486                       const char *filename,
1487                       CORE_ADDR textlow, struct partial_symbol **global_syms,
1488                       struct partial_symbol **static_syms)
1489 {
1490   struct partial_symtab *psymtab;
1491
1492   psymtab = allocate_psymtab (filename, objfile);
1493   psymtab->textlow = textlow;
1494   psymtab->texthigh = psymtab->textlow;         /* default */
1495   psymtab->globals_offset = global_syms - objfile->global_psymbols.list;
1496   psymtab->statics_offset = static_syms - objfile->static_psymbols.list;
1497   return psymtab;
1498 }
1499
1500 /* Perform "finishing up" operations of a partial symtab.  */
1501
1502 void
1503 end_psymtab_common (struct objfile *objfile, struct partial_symtab *pst)
1504 {
1505   pst->n_global_syms
1506     = objfile->global_psymbols.next - (objfile->global_psymbols.list
1507                                        + pst->globals_offset);
1508   pst->n_static_syms
1509     = objfile->static_psymbols.next - (objfile->static_psymbols.list
1510                                        + pst->statics_offset);
1511
1512   sort_pst_symbols (objfile, pst);
1513 }
1514
1515 /* Calculate a hash code for the given partial symbol.  The hash is
1516    calculated using the symbol's value, language, domain, class
1517    and name.  These are the values which are set by
1518    add_psymbol_to_bcache.  */
1519
1520 static unsigned long
1521 psymbol_hash (const void *addr, int length)
1522 {
1523   unsigned long h = 0;
1524   struct partial_symbol *psymbol = (struct partial_symbol *) addr;
1525   unsigned int lang = psymbol->ginfo.language;
1526   unsigned int domain = PSYMBOL_DOMAIN (psymbol);
1527   unsigned int theclass = PSYMBOL_CLASS (psymbol);
1528
1529   h = hash_continue (&psymbol->ginfo.value, sizeof (psymbol->ginfo.value), h);
1530   h = hash_continue (&lang, sizeof (unsigned int), h);
1531   h = hash_continue (&domain, sizeof (unsigned int), h);
1532   h = hash_continue (&theclass, sizeof (unsigned int), h);
1533   h = hash_continue (psymbol->ginfo.name, strlen (psymbol->ginfo.name), h);
1534
1535   return h;
1536 }
1537
1538 /* Returns true if the symbol at addr1 equals the symbol at addr2.
1539    For the comparison this function uses a symbols value,
1540    language, domain, class and name.  */
1541
1542 static int
1543 psymbol_compare (const void *addr1, const void *addr2, int length)
1544 {
1545   struct partial_symbol *sym1 = (struct partial_symbol *) addr1;
1546   struct partial_symbol *sym2 = (struct partial_symbol *) addr2;
1547
1548   return (memcmp (&sym1->ginfo.value, &sym2->ginfo.value,
1549                   sizeof (sym1->ginfo.value)) == 0
1550           && sym1->ginfo.language == sym2->ginfo.language
1551           && PSYMBOL_DOMAIN (sym1) == PSYMBOL_DOMAIN (sym2)
1552           && PSYMBOL_CLASS (sym1) == PSYMBOL_CLASS (sym2)
1553           && sym1->ginfo.name == sym2->ginfo.name);
1554 }
1555
1556 /* Initialize a partial symbol bcache.  */
1557
1558 struct psymbol_bcache *
1559 psymbol_bcache_init (void)
1560 {
1561   struct psymbol_bcache *bcache = XCNEW (struct psymbol_bcache);
1562
1563   bcache->bcache = bcache_xmalloc (psymbol_hash, psymbol_compare);
1564   return bcache;
1565 }
1566
1567 /* Free a partial symbol bcache.  */
1568
1569 void
1570 psymbol_bcache_free (struct psymbol_bcache *bcache)
1571 {
1572   if (bcache == NULL)
1573     return;
1574
1575   bcache_xfree (bcache->bcache);
1576   xfree (bcache);
1577 }
1578
1579 /* Return the internal bcache of the psymbol_bcache BCACHE.  */
1580
1581 struct bcache *
1582 psymbol_bcache_get_bcache (struct psymbol_bcache *bcache)
1583 {
1584   return bcache->bcache;
1585 }
1586
1587 /* Find a copy of the SYM in BCACHE.  If BCACHE has never seen this
1588    symbol before, add a copy to BCACHE.  In either case, return a pointer
1589    to BCACHE's copy of the symbol.  If optional ADDED is not NULL, return
1590    1 in case of new entry or 0 if returning an old entry.  */
1591
1592 static const struct partial_symbol *
1593 psymbol_bcache_full (struct partial_symbol *sym,
1594                      struct psymbol_bcache *bcache,
1595                      int *added)
1596 {
1597   return ((const struct partial_symbol *)
1598           bcache_full (sym, sizeof (struct partial_symbol), bcache->bcache,
1599                        added));
1600 }
1601
1602 /* Helper function, initialises partial symbol structure and stashes
1603    it into objfile's bcache.  Note that our caching mechanism will
1604    use all fields of struct partial_symbol to determine hash value of the
1605    structure.  In other words, having two symbols with the same name but
1606    different domain (or address) is possible and correct.  */
1607
1608 static const struct partial_symbol *
1609 add_psymbol_to_bcache (const char *name, int namelength, int copy_name,
1610                        domain_enum domain,
1611                        enum address_class theclass,
1612                        CORE_ADDR coreaddr,
1613                        enum language language, struct objfile *objfile,
1614                        int *added)
1615 {
1616   struct partial_symbol psymbol;
1617
1618   /* We must ensure that the entire struct has been zeroed before
1619      assigning to it, because an assignment may not touch some of the
1620      holes.  */
1621   memset (&psymbol, 0, sizeof (psymbol));
1622
1623   SYMBOL_VALUE_ADDRESS (&psymbol) = coreaddr;
1624   SYMBOL_SECTION (&psymbol) = -1;
1625   SYMBOL_SET_LANGUAGE (&psymbol, language, &objfile->objfile_obstack);
1626   PSYMBOL_DOMAIN (&psymbol) = domain;
1627   PSYMBOL_CLASS (&psymbol) = theclass;
1628
1629   SYMBOL_SET_NAMES (&psymbol, name, namelength, copy_name, objfile);
1630
1631   /* Stash the partial symbol away in the cache.  */
1632   return psymbol_bcache_full (&psymbol, objfile->psymbol_cache, added);
1633 }
1634
1635 /* Increase the space allocated for LISTP, which is probably
1636    global_psymbols or static_psymbols.  This space will eventually
1637    be freed in free_objfile().  */
1638
1639 static void
1640 extend_psymbol_list (struct psymbol_allocation_list *listp,
1641                      struct objfile *objfile)
1642 {
1643   int new_size;
1644
1645   if (listp->size == 0)
1646     {
1647       new_size = 255;
1648       listp->list = XNEWVEC (struct partial_symbol *, new_size);
1649     }
1650   else
1651     {
1652       new_size = listp->size * 2;
1653       listp->list = (struct partial_symbol **)
1654         xrealloc ((char *) listp->list,
1655                   new_size * sizeof (struct partial_symbol *));
1656     }
1657   /* Next assumes we only went one over.  Should be good if
1658      program works correctly.  */
1659   listp->next = listp->list + listp->size;
1660   listp->size = new_size;
1661 }
1662
1663 /* Helper function, adds partial symbol to the given partial symbol list.  */
1664
1665 static void
1666 append_psymbol_to_list (struct psymbol_allocation_list *list,
1667                         const struct partial_symbol *psym,
1668                         struct objfile *objfile)
1669 {
1670   if (list->next >= list->list + list->size)
1671     extend_psymbol_list (list, objfile);
1672   *list->next++ = (struct partial_symbol *) psym;
1673   OBJSTAT (objfile, n_psyms++);
1674 }
1675
1676 /* Add a symbol with a long value to a psymtab.
1677    Since one arg is a struct, we pass in a ptr and deref it (sigh).
1678    The only value we need to store for psyms is an address.
1679    For all other psyms pass zero for COREADDR.
1680    Return the partial symbol that has been added.  */
1681
1682 void
1683 add_psymbol_to_list (const char *name, int namelength, int copy_name,
1684                      domain_enum domain,
1685                      enum address_class theclass,
1686                      struct psymbol_allocation_list *list,
1687                      CORE_ADDR coreaddr,
1688                      enum language language, struct objfile *objfile)
1689 {
1690   const struct partial_symbol *psym;
1691
1692   int added;
1693
1694   /* Stash the partial symbol away in the cache.  */
1695   psym = add_psymbol_to_bcache (name, namelength, copy_name, domain, theclass,
1696                                 coreaddr, language, objfile, &added);
1697
1698   /* Do not duplicate global partial symbols.  */
1699   if (list == &objfile->global_psymbols
1700       && !added)
1701     return;
1702
1703   /* Save pointer to partial symbol in psymtab, growing symtab if needed.  */
1704   append_psymbol_to_list (list, psym, objfile);
1705 }
1706
1707 /* Initialize storage for partial symbols.  */
1708
1709 void
1710 init_psymbol_list (struct objfile *objfile, int total_symbols)
1711 {
1712   /* Free any previously allocated psymbol lists.  */
1713
1714   if (objfile->global_psymbols.list)
1715     xfree (objfile->global_psymbols.list);
1716   if (objfile->static_psymbols.list)
1717     xfree (objfile->static_psymbols.list);
1718
1719   /* Current best guess is that approximately a twentieth
1720      of the total symbols (in a debugging file) are global or static
1721      oriented symbols, then multiply that by slop factor of two.  */
1722
1723   objfile->global_psymbols.size = total_symbols / 10;
1724   objfile->static_psymbols.size = total_symbols / 10;
1725
1726   if (objfile->global_psymbols.size > 0)
1727     {
1728       objfile->global_psymbols.next =
1729         objfile->global_psymbols.list =
1730           XNEWVEC (struct partial_symbol *, objfile->global_psymbols.size);
1731     }
1732   if (objfile->static_psymbols.size > 0)
1733     {
1734       objfile->static_psymbols.next =
1735         objfile->static_psymbols.list =
1736           XNEWVEC (struct partial_symbol *, objfile->static_psymbols.size);
1737     }
1738 }
1739
1740 struct partial_symtab *
1741 allocate_psymtab (const char *filename, struct objfile *objfile)
1742 {
1743   struct partial_symtab *psymtab;
1744
1745   if (objfile->free_psymtabs)
1746     {
1747       psymtab = objfile->free_psymtabs;
1748       objfile->free_psymtabs = psymtab->next;
1749     }
1750   else
1751     psymtab = (struct partial_symtab *)
1752       obstack_alloc (&objfile->objfile_obstack,
1753                      sizeof (struct partial_symtab));
1754
1755   memset (psymtab, 0, sizeof (struct partial_symtab));
1756   psymtab->filename
1757     = (const char *) bcache (filename, strlen (filename) + 1,
1758                              objfile->per_bfd->filename_cache);
1759   psymtab->compunit_symtab = NULL;
1760
1761   /* Prepend it to the psymtab list for the objfile it belongs to.
1762      Psymtabs are searched in most recent inserted -> least recent
1763      inserted order.  */
1764
1765   psymtab->next = objfile->psymtabs;
1766   objfile->psymtabs = psymtab;
1767
1768   if (symtab_create_debug)
1769     {
1770       /* Be a bit clever with debugging messages, and don't print objfile
1771          every time, only when it changes.  */
1772       static char *last_objfile_name = NULL;
1773
1774       if (last_objfile_name == NULL
1775           || strcmp (last_objfile_name, objfile_name (objfile)) != 0)
1776         {
1777           xfree (last_objfile_name);
1778           last_objfile_name = xstrdup (objfile_name (objfile));
1779           fprintf_unfiltered (gdb_stdlog,
1780                               "Creating one or more psymtabs for objfile %s ...\n",
1781                               last_objfile_name);
1782         }
1783       fprintf_unfiltered (gdb_stdlog,
1784                           "Created psymtab %s for module %s.\n",
1785                           host_address_to_string (psymtab), filename);
1786     }
1787
1788   return psymtab;
1789 }
1790
1791 void
1792 discard_psymtab (struct objfile *objfile, struct partial_symtab *pst)
1793 {
1794   struct partial_symtab **prev_pst;
1795
1796   /* From dbxread.c:
1797      Empty psymtabs happen as a result of header files which don't
1798      have any symbols in them.  There can be a lot of them.  But this
1799      check is wrong, in that a psymtab with N_SLINE entries but
1800      nothing else is not empty, but we don't realize that.  Fixing
1801      that without slowing things down might be tricky.  */
1802
1803   /* First, snip it out of the psymtab chain.  */
1804
1805   prev_pst = &(objfile->psymtabs);
1806   while ((*prev_pst) != pst)
1807     prev_pst = &((*prev_pst)->next);
1808   (*prev_pst) = pst->next;
1809
1810   /* Next, put it on a free list for recycling.  */
1811
1812   pst->next = objfile->free_psymtabs;
1813   objfile->free_psymtabs = pst;
1814 }
1815
1816 \f
1817
1818 /* We need to pass a couple of items to the addrmap_foreach function,
1819    so use a struct.  */
1820
1821 struct dump_psymtab_addrmap_data
1822 {
1823   struct objfile *objfile;
1824   struct partial_symtab *psymtab;
1825   struct ui_file *outfile;
1826
1827   /* Non-zero if the previously printed addrmap entry was for PSYMTAB.
1828      If so, we want to print the next one as well (since the next addrmap
1829      entry defines the end of the range).  */
1830   int previous_matched;
1831 };
1832
1833 /* Helper function for dump_psymtab_addrmap to print an addrmap entry.  */
1834
1835 static int
1836 dump_psymtab_addrmap_1 (void *datap, CORE_ADDR start_addr, void *obj)
1837 {
1838   struct dump_psymtab_addrmap_data *data
1839     = (struct dump_psymtab_addrmap_data *) datap;
1840   struct gdbarch *gdbarch = get_objfile_arch (data->objfile);
1841   struct partial_symtab *addrmap_psymtab = (struct partial_symtab *) obj;
1842   const char *psymtab_address_or_end = NULL;
1843
1844   QUIT;
1845
1846   if (data->psymtab == NULL
1847       || data->psymtab == addrmap_psymtab)
1848     psymtab_address_or_end = host_address_to_string (addrmap_psymtab);
1849   else if (data->previous_matched)
1850     psymtab_address_or_end = "<ends here>";
1851
1852   if (data->psymtab == NULL
1853       || data->psymtab == addrmap_psymtab
1854       || data->previous_matched)
1855     {
1856       fprintf_filtered (data->outfile, "  %s%s %s\n",
1857                         data->psymtab != NULL ? "  " : "",
1858                         paddress (gdbarch, start_addr),
1859                         psymtab_address_or_end);
1860     }
1861
1862   data->previous_matched = (data->psymtab == NULL
1863                             || data->psymtab == addrmap_psymtab);
1864
1865   return 0;
1866 }
1867
1868 /* Helper function for maintenance_print_psymbols to print the addrmap
1869    of PSYMTAB.  If PSYMTAB is NULL print the entire addrmap.  */
1870
1871 static void
1872 dump_psymtab_addrmap (struct objfile *objfile, struct partial_symtab *psymtab,
1873                       struct ui_file *outfile)
1874 {
1875   struct dump_psymtab_addrmap_data addrmap_dump_data;
1876
1877   if ((psymtab == NULL
1878        || psymtab->psymtabs_addrmap_supported)
1879       && objfile->psymtabs_addrmap != NULL)
1880     {
1881       addrmap_dump_data.objfile = objfile;
1882       addrmap_dump_data.psymtab = psymtab;
1883       addrmap_dump_data.outfile = outfile;
1884       addrmap_dump_data.previous_matched = 0;
1885       fprintf_filtered (outfile, "%sddress map:\n",
1886                         psymtab == NULL ? "Entire a" : "  A");
1887       addrmap_foreach (objfile->psymtabs_addrmap, dump_psymtab_addrmap_1,
1888                        &addrmap_dump_data);
1889     }
1890 }
1891
1892 static void
1893 maintenance_print_psymbols (const char *args, int from_tty)
1894 {
1895   struct ui_file *outfile = gdb_stdout;
1896   char *address_arg = NULL, *source_arg = NULL, *objfile_arg = NULL;
1897   struct objfile *objfile;
1898   struct partial_symtab *ps;
1899   int i, outfile_idx, found;
1900   CORE_ADDR pc = 0;
1901   struct obj_section *section = NULL;
1902
1903   dont_repeat ();
1904
1905   gdb_argv argv (args);
1906
1907   for (i = 0; argv != NULL && argv[i] != NULL; ++i)
1908     {
1909       if (strcmp (argv[i], "-pc") == 0)
1910         {
1911           if (argv[i + 1] == NULL)
1912             error (_("Missing pc value"));
1913           address_arg = argv[++i];
1914         }
1915       else if (strcmp (argv[i], "-source") == 0)
1916         {
1917           if (argv[i + 1] == NULL)
1918             error (_("Missing source file"));
1919           source_arg = argv[++i];
1920         }
1921       else if (strcmp (argv[i], "-objfile") == 0)
1922         {
1923           if (argv[i + 1] == NULL)
1924             error (_("Missing objfile name"));
1925           objfile_arg = argv[++i];
1926         }
1927       else if (strcmp (argv[i], "--") == 0)
1928         {
1929           /* End of options.  */
1930           ++i;
1931           break;
1932         }
1933       else if (argv[i][0] == '-')
1934         {
1935           /* Future proofing: Don't allow OUTFILE to begin with "-".  */
1936           error (_("Unknown option: %s"), argv[i]);
1937         }
1938       else
1939         break;
1940     }
1941   outfile_idx = i;
1942
1943   if (address_arg != NULL && source_arg != NULL)
1944     error (_("Must specify at most one of -pc and -source"));
1945
1946   stdio_file arg_outfile;
1947
1948   if (argv != NULL && argv[outfile_idx] != NULL)
1949     {
1950       if (argv[outfile_idx + 1] != NULL)
1951         error (_("Junk at end of command"));
1952       gdb::unique_xmalloc_ptr<char> outfile_name
1953         (tilde_expand (argv[outfile_idx]));
1954       if (!arg_outfile.open (outfile_name.get (), FOPEN_WT))
1955         perror_with_name (outfile_name.get ());
1956       outfile = &arg_outfile;
1957     }
1958
1959   if (address_arg != NULL)
1960     {
1961       pc = parse_and_eval_address (address_arg);
1962       /* If we fail to find a section, that's ok, try the lookup anyway.  */
1963       section = find_pc_section (pc);
1964     }
1965
1966   found = 0;
1967   ALL_OBJFILES (objfile)
1968     {
1969       int printed_objfile_header = 0;
1970       int print_for_objfile = 1;
1971
1972       QUIT;
1973       if (objfile_arg != NULL)
1974         print_for_objfile
1975           = compare_filenames_for_search (objfile_name (objfile),
1976                                           objfile_arg);
1977       if (!print_for_objfile)
1978         continue;
1979
1980       if (address_arg != NULL)
1981         {
1982           struct bound_minimal_symbol msymbol = { NULL, NULL };
1983
1984           /* We don't assume each pc has a unique objfile (this is for
1985              debugging).  */
1986           ps = find_pc_sect_psymtab (objfile, pc, section, msymbol);
1987           if (ps != NULL)
1988             {
1989               if (!printed_objfile_header)
1990                 {
1991                   outfile->printf ("\nPartial symtabs for objfile %s\n",
1992                                   objfile_name (objfile));
1993                   printed_objfile_header = 1;
1994                 }
1995               dump_psymtab (objfile, ps, outfile);
1996               dump_psymtab_addrmap (objfile, ps, outfile);
1997               found = 1;
1998             }
1999         }
2000       else
2001         {
2002           ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
2003             {
2004               int print_for_source = 0;
2005
2006               QUIT;
2007               if (source_arg != NULL)
2008                 {
2009                   print_for_source
2010                     = compare_filenames_for_search (ps->filename, source_arg);
2011                   found = 1;
2012                 }
2013               if (source_arg == NULL
2014                   || print_for_source)
2015                 {
2016                   if (!printed_objfile_header)
2017                     {
2018                       outfile->printf ("\nPartial symtabs for objfile %s\n",
2019                                        objfile_name (objfile));
2020                       printed_objfile_header = 1;
2021                     }
2022                   dump_psymtab (objfile, ps, outfile);
2023                   dump_psymtab_addrmap (objfile, ps, outfile);
2024                 }
2025             }
2026         }
2027
2028       /* If we're printing all the objfile's symbols dump the full addrmap.  */
2029
2030       if (address_arg == NULL
2031           && source_arg == NULL
2032           && objfile->psymtabs_addrmap != NULL)
2033         {
2034           outfile->puts ("\n");
2035           dump_psymtab_addrmap (objfile, NULL, outfile);
2036         }
2037     }
2038
2039   if (!found)
2040     {
2041       if (address_arg != NULL)
2042         error (_("No partial symtab for address: %s"), address_arg);
2043       if (source_arg != NULL)
2044         error (_("No partial symtab for source file: %s"), source_arg);
2045     }
2046 }
2047
2048 /* List all the partial symbol tables whose names match REGEXP (optional).  */
2049
2050 static void
2051 maintenance_info_psymtabs (const char *regexp, int from_tty)
2052 {
2053   struct program_space *pspace;
2054   struct objfile *objfile;
2055
2056   if (regexp)
2057     re_comp (regexp);
2058
2059   ALL_PSPACES (pspace)
2060     ALL_PSPACE_OBJFILES (pspace, objfile)
2061     {
2062       struct gdbarch *gdbarch = get_objfile_arch (objfile);
2063       struct partial_symtab *psymtab;
2064
2065       /* We don't want to print anything for this objfile until we
2066          actually find a symtab whose name matches.  */
2067       int printed_objfile_start = 0;
2068
2069       ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, psymtab)
2070         {
2071           QUIT;
2072
2073           if (! regexp
2074               || re_exec (psymtab->filename))
2075             {
2076               if (! printed_objfile_start)
2077                 {
2078                   printf_filtered ("{ objfile %s ", objfile_name (objfile));
2079                   wrap_here ("  ");
2080                   printf_filtered ("((struct objfile *) %s)\n",
2081                                    host_address_to_string (objfile));
2082                   printed_objfile_start = 1;
2083                 }
2084
2085               printf_filtered ("  { psymtab %s ", psymtab->filename);
2086               wrap_here ("    ");
2087               printf_filtered ("((struct partial_symtab *) %s)\n",
2088                                host_address_to_string (psymtab));
2089
2090               printf_filtered ("    readin %s\n",
2091                                psymtab->readin ? "yes" : "no");
2092               printf_filtered ("    fullname %s\n",
2093                                psymtab->fullname
2094                                ? psymtab->fullname : "(null)");
2095               printf_filtered ("    text addresses ");
2096               fputs_filtered (paddress (gdbarch, psymtab->textlow),
2097                               gdb_stdout);
2098               printf_filtered (" -- ");
2099               fputs_filtered (paddress (gdbarch, psymtab->texthigh),
2100                               gdb_stdout);
2101               printf_filtered ("\n");
2102               printf_filtered ("    psymtabs_addrmap_supported %s\n",
2103                                (psymtab->psymtabs_addrmap_supported
2104                                 ? "yes" : "no"));
2105               printf_filtered ("    globals ");
2106               if (psymtab->n_global_syms)
2107                 {
2108                   printf_filtered ("(* (struct partial_symbol **) %s @ %d)\n",
2109                                    host_address_to_string (objfile->global_psymbols.list
2110                                     + psymtab->globals_offset),
2111                                    psymtab->n_global_syms);
2112                 }
2113               else
2114                 printf_filtered ("(none)\n");
2115               printf_filtered ("    statics ");
2116               if (psymtab->n_static_syms)
2117                 {
2118                   printf_filtered ("(* (struct partial_symbol **) %s @ %d)\n",
2119                                    host_address_to_string (objfile->static_psymbols.list
2120                                     + psymtab->statics_offset),
2121                                    psymtab->n_static_syms);
2122                 }
2123               else
2124                 printf_filtered ("(none)\n");
2125               printf_filtered ("    dependencies ");
2126               if (psymtab->number_of_dependencies)
2127                 {
2128                   int i;
2129
2130                   printf_filtered ("{\n");
2131                   for (i = 0; i < psymtab->number_of_dependencies; i++)
2132                     {
2133                       struct partial_symtab *dep = psymtab->dependencies[i];
2134
2135                       /* Note the string concatenation there --- no comma.  */
2136                       printf_filtered ("      psymtab %s "
2137                                        "((struct partial_symtab *) %s)\n",
2138                                        dep->filename,
2139                                        host_address_to_string (dep));
2140                     }
2141                   printf_filtered ("    }\n");
2142                 }
2143               else
2144                 printf_filtered ("(none)\n");
2145               printf_filtered ("  }\n");
2146             }
2147         }
2148
2149       if (printed_objfile_start)
2150         printf_filtered ("}\n");
2151     }
2152 }
2153
2154 /* Check consistency of currently expanded psymtabs vs symtabs.  */
2155
2156 static void
2157 maintenance_check_psymtabs (const char *ignore, int from_tty)
2158 {
2159   struct symbol *sym;
2160   struct partial_symbol **psym;
2161   struct compunit_symtab *cust = NULL;
2162   struct partial_symtab *ps;
2163   const struct blockvector *bv;
2164   struct objfile *objfile;
2165   struct block *b;
2166   int length;
2167
2168   ALL_PSYMTABS (objfile, ps)
2169   {
2170     struct gdbarch *gdbarch = get_objfile_arch (objfile);
2171
2172     /* We don't call psymtab_to_symtab here because that may cause symtab
2173        expansion.  When debugging a problem it helps if checkers leave
2174        things unchanged.  */
2175     cust = ps->compunit_symtab;
2176
2177     /* First do some checks that don't require the associated symtab.  */
2178     if (ps->texthigh < ps->textlow)
2179       {
2180         printf_filtered ("Psymtab ");
2181         puts_filtered (ps->filename);
2182         printf_filtered (" covers bad range ");
2183         fputs_filtered (paddress (gdbarch, ps->textlow), gdb_stdout);
2184         printf_filtered (" - ");
2185         fputs_filtered (paddress (gdbarch, ps->texthigh), gdb_stdout);
2186         printf_filtered ("\n");
2187         continue;
2188       }
2189
2190     /* Now do checks requiring the associated symtab.  */
2191     if (cust == NULL)
2192       continue;
2193     bv = COMPUNIT_BLOCKVECTOR (cust);
2194     b = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
2195     psym = objfile->static_psymbols.list + ps->statics_offset;
2196     length = ps->n_static_syms;
2197     while (length--)
2198       {
2199         sym = block_lookup_symbol (b, SYMBOL_LINKAGE_NAME (*psym),
2200                                    SYMBOL_DOMAIN (*psym));
2201         if (!sym)
2202           {
2203             printf_filtered ("Static symbol `");
2204             puts_filtered (SYMBOL_LINKAGE_NAME (*psym));
2205             printf_filtered ("' only found in ");
2206             puts_filtered (ps->filename);
2207             printf_filtered (" psymtab\n");
2208           }
2209         psym++;
2210       }
2211     b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2212     psym = objfile->global_psymbols.list + ps->globals_offset;
2213     length = ps->n_global_syms;
2214     while (length--)
2215       {
2216         sym = block_lookup_symbol (b, SYMBOL_LINKAGE_NAME (*psym),
2217                                    SYMBOL_DOMAIN (*psym));
2218         if (!sym)
2219           {
2220             printf_filtered ("Global symbol `");
2221             puts_filtered (SYMBOL_LINKAGE_NAME (*psym));
2222             printf_filtered ("' only found in ");
2223             puts_filtered (ps->filename);
2224             printf_filtered (" psymtab\n");
2225           }
2226         psym++;
2227       }
2228     if (ps->texthigh != 0
2229         && (ps->textlow < BLOCK_START (b) || ps->texthigh > BLOCK_END (b)))
2230       {
2231         printf_filtered ("Psymtab ");
2232         puts_filtered (ps->filename);
2233         printf_filtered (" covers ");
2234         fputs_filtered (paddress (gdbarch, ps->textlow), gdb_stdout);
2235         printf_filtered (" - ");
2236         fputs_filtered (paddress (gdbarch, ps->texthigh), gdb_stdout);
2237         printf_filtered (" but symtab covers only ");
2238         fputs_filtered (paddress (gdbarch, BLOCK_START (b)), gdb_stdout);
2239         printf_filtered (" - ");
2240         fputs_filtered (paddress (gdbarch, BLOCK_END (b)), gdb_stdout);
2241         printf_filtered ("\n");
2242       }
2243   }
2244 }
2245
2246 void
2247 _initialize_psymtab (void)
2248 {
2249   add_cmd ("psymbols", class_maintenance, maintenance_print_psymbols, _("\
2250 Print dump of current partial symbol definitions.\n\
2251 Usage: mt print psymbols [-objfile objfile] [-pc address] [--] [outfile]\n\
2252        mt print psymbols [-objfile objfile] [-source source] [--] [outfile]\n\
2253 Entries in the partial symbol table are dumped to file OUTFILE,\n\
2254 or the terminal if OUTFILE is unspecified.\n\
2255 If ADDRESS is provided, dump only the file for that address.\n\
2256 If SOURCE is provided, dump only that file's symbols.\n\
2257 If OBJFILE is provided, dump only that file's minimal symbols."),
2258            &maintenanceprintlist);
2259
2260   add_cmd ("psymtabs", class_maintenance, maintenance_info_psymtabs, _("\
2261 List the partial symbol tables for all object files.\n\
2262 This does not include information about individual partial symbols,\n\
2263 just the symbol table structures themselves."),
2264            &maintenanceinfolist);
2265
2266   add_cmd ("check-psymtabs", class_maintenance, maintenance_check_psymtabs,
2267            _("\
2268 Check consistency of currently expanded psymtabs versus symtabs."),
2269            &maintenancelist);
2270 }