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