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