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