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