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