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