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