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