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