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