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