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