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