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