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