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