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