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