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