xref: /netbsd/external/gpl3/gdb/dist/gdb/psymtab.c (revision 1424dfb3)
1 /* Partial symbol tables.
2 
3    Copyright (C) 2009-2020 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 "objfiles.h"
23 #include "psympriv.h"
24 #include "block.h"
25 #include "filenames.h"
26 #include "source.h"
27 #include "addrmap.h"
28 #include "gdbtypes.h"
29 #include "ui-out.h"
30 #include "command.h"
31 #include "readline/tilde.h"
32 #include "gdb_regex.h"
33 #include "dictionary.h"
34 #include "language.h"
35 #include "cp-support.h"
36 #include "gdbcmd.h"
37 #include <algorithm>
38 #include <set>
39 
40 static struct partial_symbol *lookup_partial_symbol (struct objfile *,
41 						     struct partial_symtab *,
42 						     const lookup_name_info &,
43 						     int,
44 						     domain_enum);
45 
46 static const char *psymtab_to_fullname (struct partial_symtab *ps);
47 
48 static struct partial_symbol *find_pc_sect_psymbol (struct objfile *,
49 						    struct partial_symtab *,
50 						    CORE_ADDR,
51 						    struct obj_section *);
52 
53 static struct compunit_symtab *psymtab_to_symtab (struct objfile *objfile,
54 						  struct partial_symtab *pst);
55 
56 
57 
58 static unsigned long psymbol_hash (const void *addr, int length);
59 static int psymbol_compare (const void *addr1, const void *addr2, int length);
60 
psymtab_storage()61 psymtab_storage::psymtab_storage ()
62   : psymbol_cache (psymbol_hash, psymbol_compare)
63 {
64 }
65 
~psymtab_storage()66 psymtab_storage::~psymtab_storage ()
67 {
68   partial_symtab *iter = psymtabs;
69   while (iter != nullptr)
70     {
71       partial_symtab *next = iter->next;
72       delete iter;
73       iter = next;
74     }
75 }
76 
77 /* See psymtab.h.  */
78 
79 void
install_psymtab(partial_symtab * pst)80 psymtab_storage::install_psymtab (partial_symtab *pst)
81 {
82   pst->next = psymtabs;
83   psymtabs = pst;
84 }
85 
86 
87 
88 /* See psymtab.h.  */
89 
90 psymtab_storage::partial_symtab_range
require_partial_symbols(struct objfile * objfile,bool verbose)91 require_partial_symbols (struct objfile *objfile, bool verbose)
92 {
93   if ((objfile->flags & OBJF_PSYMTABS_READ) == 0)
94     {
95       objfile->flags |= OBJF_PSYMTABS_READ;
96 
97       if (objfile->sf->sym_read_psymbols)
98 	{
99 	  if (verbose)
100 	    printf_filtered (_("Reading symbols from %s...\n"),
101 			     objfile_name (objfile));
102 	  (*objfile->sf->sym_read_psymbols) (objfile);
103 
104 	  /* Partial symbols list are not expected to changed after this
105 	     point.  */
106 	  objfile->partial_symtabs->global_psymbols.shrink_to_fit ();
107 	  objfile->partial_symtabs->static_psymbols.shrink_to_fit ();
108 
109 	  if (verbose && !objfile_has_symbols (objfile))
110 	    printf_filtered (_("(No debugging symbols found in %s)\n"),
111 			     objfile_name (objfile));
112 	}
113     }
114 
115   return objfile->psymtabs ();
116 }
117 
118 /* Helper function for psym_map_symtabs_matching_filename that
119    expands the symtabs and calls the iterator.  */
120 
121 static bool
partial_map_expand_apply(struct objfile * objfile,const char * name,const char * real_path,struct partial_symtab * pst,gdb::function_view<bool (symtab *)> callback)122 partial_map_expand_apply (struct objfile *objfile,
123 			  const char *name,
124 			  const char *real_path,
125 			  struct partial_symtab *pst,
126 			  gdb::function_view<bool (symtab *)> callback)
127 {
128   struct compunit_symtab *last_made = objfile->compunit_symtabs;
129 
130   /* Shared psymtabs should never be seen here.  Instead they should
131      be handled properly by the caller.  */
132   gdb_assert (pst->user == NULL);
133 
134   /* Don't visit already-expanded psymtabs.  */
135   if (pst->readin_p (objfile))
136     return 0;
137 
138   /* This may expand more than one symtab, and we want to iterate over
139      all of them.  */
140   psymtab_to_symtab (objfile, pst);
141 
142   return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
143 				    last_made, callback);
144 }
145 
146 /*  Psymtab version of map_symtabs_matching_filename.  See its definition in
147     the definition of quick_symbol_functions in symfile.h.  */
148 
149 static bool
psym_map_symtabs_matching_filename(struct objfile * objfile,const char * name,const char * real_path,gdb::function_view<bool (symtab *)> callback)150 psym_map_symtabs_matching_filename
151   (struct objfile *objfile,
152    const char *name,
153    const char *real_path,
154    gdb::function_view<bool (symtab *)> callback)
155 {
156   const char *name_basename = lbasename (name);
157 
158   for (partial_symtab *pst : require_partial_symbols (objfile, true))
159     {
160       /* Anonymous psymtabs don't have a file name.  */
161       if (pst->anonymous)
162 	continue;
163 
164       if (compare_filenames_for_search (pst->filename, name))
165 	{
166 	  while (pst->user)
167 	    pst = pst->user;
168 
169 	  if (partial_map_expand_apply (objfile, name, real_path,
170 					pst, callback))
171 	    return true;
172 	  continue;
173 	}
174 
175       /* Before we invoke realpath, which can get expensive when many
176 	 files are involved, do a quick comparison of the basenames.  */
177       if (! basenames_may_differ
178 	  && FILENAME_CMP (name_basename, lbasename (pst->filename)) != 0)
179 	continue;
180 
181       if (compare_filenames_for_search (psymtab_to_fullname (pst), name))
182 	{
183 	  if (partial_map_expand_apply (objfile, name, real_path,
184 					pst, callback))
185 	    return true;
186 	  continue;
187 	}
188 
189       /* If the user gave us an absolute path, try to find the file in
190 	 this symtab and use its absolute path.  */
191       if (real_path != NULL)
192 	{
193 	  gdb_assert (IS_ABSOLUTE_PATH (real_path));
194 	  gdb_assert (IS_ABSOLUTE_PATH (name));
195 	  if (filename_cmp (psymtab_to_fullname (pst), real_path) == 0)
196 	    {
197 	      if (partial_map_expand_apply (objfile, name, real_path,
198 					    pst, callback))
199 		return true;
200 	      continue;
201 	    }
202 	}
203     }
204 
205   return false;
206 }
207 
208 /* Find which partial symtab contains PC and SECTION starting at psymtab PST.
209    We may find a different psymtab than PST.  See FIND_PC_SECT_PSYMTAB.  */
210 
211 static struct partial_symtab *
find_pc_sect_psymtab_closer(struct objfile * objfile,CORE_ADDR pc,struct obj_section * section,struct partial_symtab * pst,struct bound_minimal_symbol msymbol)212 find_pc_sect_psymtab_closer (struct objfile *objfile,
213 			     CORE_ADDR pc, struct obj_section *section,
214 			     struct partial_symtab *pst,
215 			     struct bound_minimal_symbol msymbol)
216 {
217   struct partial_symtab *tpst;
218   struct partial_symtab *best_pst = pst;
219   CORE_ADDR best_addr = pst->text_low (objfile);
220 
221   gdb_assert (!pst->psymtabs_addrmap_supported);
222 
223   /* An objfile that has its functions reordered might have
224      many partial symbol tables containing the PC, but
225      we want the partial symbol table that contains the
226      function containing the PC.  */
227   if (!(objfile->flags & OBJF_REORDERED)
228       && section == NULL)  /* Can't validate section this way.  */
229     return pst;
230 
231   if (msymbol.minsym == NULL)
232     return pst;
233 
234   /* The code range of partial symtabs sometimes overlap, so, in
235      the loop below, we need to check all partial symtabs and
236      find the one that fits better for the given PC address.  We
237      select the partial symtab that contains a symbol whose
238      address is closest to the PC address.  By closest we mean
239      that find_pc_sect_symbol returns the symbol with address
240      that is closest and still less than the given PC.  */
241   for (tpst = pst; tpst != NULL; tpst = tpst->next)
242     {
243       if (pc >= tpst->text_low (objfile) && pc < tpst->text_high (objfile))
244 	{
245 	  struct partial_symbol *p;
246 	  CORE_ADDR this_addr;
247 
248 	  /* NOTE: This assumes that every psymbol has a
249 	     corresponding msymbol, which is not necessarily
250 	     true; the debug info might be much richer than the
251 	     object's symbol table.  */
252 	  p = find_pc_sect_psymbol (objfile, tpst, pc, section);
253 	  if (p != NULL
254 	      && (p->address (objfile) == BMSYMBOL_VALUE_ADDRESS (msymbol)))
255 	    return tpst;
256 
257 	  /* Also accept the textlow value of a psymtab as a
258 	     "symbol", to provide some support for partial
259 	     symbol tables with line information but no debug
260 	     symbols (e.g. those produced by an assembler).  */
261 	  if (p != NULL)
262 	    this_addr = p->address (objfile);
263 	  else
264 	    this_addr = tpst->text_low (objfile);
265 
266 	  /* Check whether it is closer than our current
267 	     BEST_ADDR.  Since this symbol address is
268 	     necessarily lower or equal to PC, the symbol closer
269 	     to PC is the symbol which address is the highest.
270 	     This way we return the psymtab which contains such
271 	     best match symbol.  This can help in cases where the
272 	     symbol information/debuginfo is not complete, like
273 	     for instance on IRIX6 with gcc, where no debug info
274 	     is emitted for statics.  (See also the nodebug.exp
275 	     testcase.)  */
276 	  if (this_addr > best_addr)
277 	    {
278 	      best_addr = this_addr;
279 	      best_pst = tpst;
280 	    }
281 	}
282     }
283   return best_pst;
284 }
285 
286 /* Find which partial symtab contains PC and SECTION.  Return NULL if
287    none.  We return the psymtab that contains a symbol whose address
288    exactly matches PC, or, if we cannot find an exact match, the
289    psymtab that contains a symbol whose address is closest to PC.  */
290 
291 static struct partial_symtab *
find_pc_sect_psymtab(struct objfile * objfile,CORE_ADDR pc,struct obj_section * section,struct bound_minimal_symbol msymbol)292 find_pc_sect_psymtab (struct objfile *objfile, CORE_ADDR pc,
293 		      struct obj_section *section,
294 		      struct bound_minimal_symbol msymbol)
295 {
296   /* Try just the PSYMTABS_ADDRMAP mapping first as it has better
297      granularity than the later used TEXTLOW/TEXTHIGH one.  However, we need
298      to take care as the PSYMTABS_ADDRMAP can hold things other than partial
299      symtabs in some cases.
300 
301      This function should only be called for objfiles that are using partial
302      symtabs, not for objfiles that are using indexes (.gdb_index or
303      .debug_names), however 'maintenance print psymbols' calls this function
304      directly for all objfiles.  If we assume that PSYMTABS_ADDRMAP contains
305      partial symtabs then we will end up returning a pointer to an object
306      that is not a partial_symtab, which doesn't end well.  */
307 
308   if (objfile->partial_symtabs->psymtabs != NULL
309       && objfile->partial_symtabs->psymtabs_addrmap != NULL)
310     {
311       CORE_ADDR baseaddr = objfile->text_section_offset ();
312 
313       struct partial_symtab *pst
314 	= ((struct partial_symtab *)
315 	   addrmap_find (objfile->partial_symtabs->psymtabs_addrmap,
316 			 pc - baseaddr));
317       if (pst != NULL)
318 	{
319 	  /* FIXME: addrmaps currently do not handle overlayed sections,
320 	     so fall back to the non-addrmap case if we're debugging
321 	     overlays and the addrmap returned the wrong section.  */
322 	  if (overlay_debugging && msymbol.minsym != NULL && section != NULL)
323 	    {
324 	      struct partial_symbol *p;
325 
326 	      /* NOTE: This assumes that every psymbol has a
327 		 corresponding msymbol, which is not necessarily
328 		 true; the debug info might be much richer than the
329 		 object's symbol table.  */
330 	      p = find_pc_sect_psymbol (objfile, pst, pc, section);
331 	      if (p == NULL
332 		  || (p->address (objfile)
333 		      != BMSYMBOL_VALUE_ADDRESS (msymbol)))
334 		goto next;
335 	    }
336 
337 	  /* We do not try to call FIND_PC_SECT_PSYMTAB_CLOSER as
338 	     PSYMTABS_ADDRMAP we used has already the best 1-byte
339 	     granularity and FIND_PC_SECT_PSYMTAB_CLOSER may mislead us into
340 	     a worse chosen section due to the TEXTLOW/TEXTHIGH ranges
341 	     overlap.  */
342 
343 	  return pst;
344 	}
345     }
346 
347  next:
348 
349   /* Existing PSYMTABS_ADDRMAP mapping is present even for PARTIAL_SYMTABs
350      which still have no corresponding full SYMTABs read.  But it is not
351      present for non-DWARF2 debug infos not supporting PSYMTABS_ADDRMAP in GDB
352      so far.  */
353 
354   /* Check even OBJFILE with non-zero PSYMTABS_ADDRMAP as only several of
355      its CUs may be missing in PSYMTABS_ADDRMAP as they may be varying
356      debug info type in single OBJFILE.  */
357 
358   for (partial_symtab *pst : require_partial_symbols (objfile, true))
359     if (!pst->psymtabs_addrmap_supported
360 	&& pc >= pst->text_low (objfile) && pc < pst->text_high (objfile))
361       {
362 	struct partial_symtab *best_pst;
363 
364 	best_pst = find_pc_sect_psymtab_closer (objfile, pc, section, pst,
365 						msymbol);
366 	if (best_pst != NULL)
367 	  return best_pst;
368       }
369 
370   return NULL;
371 }
372 
373 /* Psymtab version of find_pc_sect_compunit_symtab.  See its definition in
374    the definition of quick_symbol_functions in symfile.h.  */
375 
376 static struct compunit_symtab *
psym_find_pc_sect_compunit_symtab(struct objfile * objfile,struct bound_minimal_symbol msymbol,CORE_ADDR pc,struct obj_section * section,int warn_if_readin)377 psym_find_pc_sect_compunit_symtab (struct objfile *objfile,
378 				   struct bound_minimal_symbol msymbol,
379 				   CORE_ADDR pc,
380 				   struct obj_section *section,
381 				   int warn_if_readin)
382 {
383   struct partial_symtab *ps = find_pc_sect_psymtab (objfile, pc, section,
384 						    msymbol);
385   if (ps != NULL)
386     {
387       if (warn_if_readin && ps->readin_p (objfile))
388 	/* Might want to error() here (in case symtab is corrupt and
389 	   will cause a core dump), but maybe we can successfully
390 	   continue, so let's not.  */
391 	warning (_("\
392 (Internal error: pc %s in read in psymtab, but not in symtab.)\n"),
393 		 paddress (objfile->arch (), pc));
394       psymtab_to_symtab (objfile, ps);
395       return ps->get_compunit_symtab (objfile);
396     }
397   return NULL;
398 }
399 
400 /* Find which partial symbol within a psymtab matches PC and SECTION.
401    Return NULL if none.  */
402 
403 static struct partial_symbol *
find_pc_sect_psymbol(struct objfile * objfile,struct partial_symtab * psymtab,CORE_ADDR pc,struct obj_section * section)404 find_pc_sect_psymbol (struct objfile *objfile,
405 		      struct partial_symtab *psymtab, CORE_ADDR pc,
406 		      struct obj_section *section)
407 {
408   struct partial_symbol *best = NULL;
409   CORE_ADDR best_pc;
410   const CORE_ADDR textlow = psymtab->text_low (objfile);
411 
412   gdb_assert (psymtab != NULL);
413 
414   /* Cope with programs that start at address 0.  */
415   best_pc = (textlow != 0) ? textlow - 1 : 0;
416 
417   /* Search the global symbols as well as the static symbols, so that
418      find_pc_partial_function doesn't use a minimal symbol and thus
419      cache a bad endaddr.  */
420   for (int i = 0; i < psymtab->n_global_syms; i++)
421     {
422       partial_symbol *p
423 	= objfile->partial_symtabs->global_psymbols[psymtab->globals_offset
424 						    + i];
425 
426       if (p->domain == VAR_DOMAIN
427 	  && p->aclass == LOC_BLOCK
428 	  && pc >= p->address (objfile)
429 	  && (p->address (objfile) > best_pc
430 	      || (psymtab->text_low (objfile) == 0
431 		  && best_pc == 0 && p->address (objfile) == 0)))
432 	{
433 	  if (section != NULL)  /* Match on a specific section.  */
434 	    {
435 	      if (!matching_obj_sections (p->obj_section (objfile),
436 					  section))
437 		continue;
438 	    }
439 	  best_pc = p->address (objfile);
440 	  best = p;
441 	}
442     }
443 
444   for (int i = 0; i < psymtab->n_static_syms; i++)
445     {
446       partial_symbol *p
447 	= objfile->partial_symtabs->static_psymbols[psymtab->statics_offset
448 						    + i];
449 
450       if (p->domain == VAR_DOMAIN
451 	  && p->aclass == LOC_BLOCK
452 	  && pc >= p->address (objfile)
453 	  && (p->address (objfile) > best_pc
454 	      || (psymtab->text_low (objfile) == 0
455 		  && best_pc == 0 && p->address (objfile) == 0)))
456 	{
457 	  if (section != NULL)  /* Match on a specific section.  */
458 	    {
459 	      if (!matching_obj_sections (p->obj_section (objfile),
460 					  section))
461 		continue;
462 	    }
463 	  best_pc = p->address (objfile);
464 	  best = p;
465 	}
466     }
467 
468   return best;
469 }
470 
471 /* Psymtab version of lookup_symbol.  See its definition in
472    the definition of quick_symbol_functions in symfile.h.  */
473 
474 static struct compunit_symtab *
psym_lookup_symbol(struct objfile * objfile,block_enum block_index,const char * name,const domain_enum domain)475 psym_lookup_symbol (struct objfile *objfile,
476 		    block_enum block_index, const char *name,
477 		    const domain_enum domain)
478 {
479   const int psymtab_index = (block_index == GLOBAL_BLOCK ? 1 : 0);
480   struct compunit_symtab *stab_best = NULL;
481 
482   lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
483 
484   lookup_name_info psym_lookup_name = lookup_name.make_ignore_params ();
485 
486   for (partial_symtab *ps : require_partial_symbols (objfile, true))
487     {
488       if (!ps->readin_p (objfile)
489 	  && lookup_partial_symbol (objfile, ps, psym_lookup_name,
490 				    psymtab_index, domain))
491 	{
492 	  struct symbol *sym, *with_opaque = NULL;
493 	  struct compunit_symtab *stab = psymtab_to_symtab (objfile, ps);
494 	  /* Note: While psymtab_to_symtab can return NULL if the
495 	     partial symtab is empty, we can assume it won't here
496 	     because lookup_partial_symbol succeeded.  */
497 	  const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
498 	  const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
499 
500 	  sym = block_find_symbol (block, name, domain,
501 				   block_find_non_opaque_type_preferred,
502 				   &with_opaque);
503 
504 	  /* Some caution must be observed with overloaded functions
505 	     and methods, since the index will not contain any overload
506 	     information (but NAME might contain it).  */
507 
508 	  if (sym != NULL
509 	      && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
510 	    return stab;
511 	  if (with_opaque != NULL
512 	      && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
513 	    stab_best = stab;
514 
515 	  /* Keep looking through other psymtabs.  */
516 	}
517     }
518 
519   return stab_best;
520 }
521 
522 /* Psymtab version of lookup_global_symbol_language.  See its definition in
523    the definition of quick_symbol_functions in symfile.h.  */
524 
525 static enum language
psym_lookup_global_symbol_language(struct objfile * objfile,const char * name,domain_enum domain,bool * symbol_found_p)526 psym_lookup_global_symbol_language (struct objfile *objfile, const char *name,
527 				    domain_enum domain, bool *symbol_found_p)
528 {
529   *symbol_found_p = false;
530   if (objfile->sf == NULL)
531     return language_unknown;
532 
533   lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
534 
535   for (partial_symtab *ps : require_partial_symbols (objfile, true))
536     {
537       struct partial_symbol *psym;
538       if (ps->readin_p (objfile))
539 	continue;
540 
541       psym = lookup_partial_symbol (objfile, ps, lookup_name, 1, domain);
542       if (psym)
543 	{
544 	  *symbol_found_p = true;
545 	  return psym->ginfo.language ();
546 	}
547     }
548 
549   return language_unknown;
550 }
551 
552 /* Returns true if PSYM matches LOOKUP_NAME.  */
553 
554 static bool
psymbol_name_matches(partial_symbol * psym,const lookup_name_info & lookup_name)555 psymbol_name_matches (partial_symbol *psym,
556 		      const lookup_name_info &lookup_name)
557 {
558   const language_defn *lang = language_def (psym->ginfo.language ());
559   symbol_name_matcher_ftype *name_match
560     = lang->get_symbol_name_matcher (lookup_name);
561   return name_match (psym->ginfo.search_name (), lookup_name, NULL);
562 }
563 
564 /* Look in PST for a symbol in DOMAIN whose name matches NAME.  Search
565    the global block of PST if GLOBAL, and otherwise the static block.
566    MATCH is the comparison operation that returns true iff MATCH (s,
567    NAME), where s is a SYMBOL_SEARCH_NAME.  If ORDERED_COMPARE is
568    non-null, the symbols in the block are assumed to be ordered
569    according to it (allowing binary search).  It must be compatible
570    with MATCH.  Returns the symbol, if found, and otherwise NULL.  */
571 
572 static struct partial_symbol *
match_partial_symbol(struct objfile * objfile,struct partial_symtab * pst,int global,const lookup_name_info & name,domain_enum domain,symbol_compare_ftype * ordered_compare)573 match_partial_symbol (struct objfile *objfile,
574 		      struct partial_symtab *pst, int global,
575 		      const lookup_name_info &name, domain_enum domain,
576 		      symbol_compare_ftype *ordered_compare)
577 {
578   struct partial_symbol **start, **psym;
579   struct partial_symbol **top, **real_top, **bottom, **center;
580   int length = (global ? pst->n_global_syms : pst->n_static_syms);
581   int do_linear_search = 1;
582 
583   if (length == 0)
584     return NULL;
585 
586   start = (global ?
587 	   &objfile->partial_symtabs->global_psymbols[pst->globals_offset] :
588 	   &objfile->partial_symtabs->static_psymbols[pst->statics_offset]);
589 
590   if (global && ordered_compare)  /* Can use a binary search.  */
591     {
592       do_linear_search = 0;
593 
594       /* Binary search.  This search is guaranteed to end with center
595          pointing at the earliest partial symbol whose name might be
596          correct.  At that point *all* partial symbols with an
597          appropriate name will be checked against the correct
598          domain.  */
599 
600       bottom = start;
601       top = start + length - 1;
602       real_top = top;
603       while (top > bottom)
604 	{
605 	  center = bottom + (top - bottom) / 2;
606 	  gdb_assert (center < top);
607 
608 	  enum language lang = (*center)->ginfo.language ();
609 	  const char *lang_ln = name.language_lookup_name (lang);
610 
611 	  if (ordered_compare ((*center)->ginfo.search_name (),
612 			       lang_ln) >= 0)
613 	    top = center;
614 	  else
615 	    bottom = center + 1;
616 	}
617       gdb_assert (top == bottom);
618 
619       while (top <= real_top
620 	     && psymbol_name_matches (*top, name))
621 	{
622 	  if (symbol_matches_domain ((*top)->ginfo.language (),
623 				     (*top)->domain, domain))
624 	    return *top;
625 	  top++;
626 	}
627     }
628 
629   /* Can't use a binary search or else we found during the binary search that
630      we should also do a linear search.  */
631 
632   if (do_linear_search)
633     {
634       for (psym = start; psym < start + length; psym++)
635 	{
636 	  if (symbol_matches_domain ((*psym)->ginfo.language (),
637 				     (*psym)->domain, domain)
638 	      && psymbol_name_matches (*psym, name))
639 	    return *psym;
640 	}
641     }
642 
643   return NULL;
644 }
645 
646 /* Look, in partial_symtab PST, for symbol whose natural name is
647    LOOKUP_NAME.  Check the global symbols if GLOBAL, the static
648    symbols if not.  */
649 
650 static struct partial_symbol *
lookup_partial_symbol(struct objfile * objfile,struct partial_symtab * pst,const lookup_name_info & lookup_name,int global,domain_enum domain)651 lookup_partial_symbol (struct objfile *objfile,
652 		       struct partial_symtab *pst,
653 		       const lookup_name_info &lookup_name,
654 		       int global, domain_enum domain)
655 {
656   struct partial_symbol **start, **psym;
657   struct partial_symbol **top, **real_top, **bottom, **center;
658   int length = (global ? pst->n_global_syms : pst->n_static_syms);
659   int do_linear_search = 1;
660 
661   if (length == 0)
662     return NULL;
663 
664   start = (global ?
665 	   &objfile->partial_symtabs->global_psymbols[pst->globals_offset] :
666 	   &objfile->partial_symtabs->static_psymbols[pst->statics_offset]);
667 
668   if (global)			/* This means we can use a binary search.  */
669     {
670       do_linear_search = 0;
671 
672       /* Binary search.  This search is guaranteed to end with center
673          pointing at the earliest partial symbol whose name might be
674          correct.  At that point *all* partial symbols with an
675          appropriate name will be checked against the correct
676          domain.  */
677 
678       bottom = start;
679       top = start + length - 1;
680       real_top = top;
681       while (top > bottom)
682 	{
683 	  center = bottom + (top - bottom) / 2;
684 
685 	  gdb_assert (center < top);
686 
687 	  if (strcmp_iw_ordered ((*center)->ginfo.search_name (),
688 				 lookup_name.c_str ()) >= 0)
689 	    {
690 	      top = center;
691 	    }
692 	  else
693 	    {
694 	      bottom = center + 1;
695 	    }
696 	}
697 
698       gdb_assert (top == bottom);
699 
700       /* For `case_sensitivity == case_sensitive_off' strcmp_iw_ordered will
701 	 search more exactly than what matches SYMBOL_MATCHES_SEARCH_NAME.  */
702       while (top >= start && symbol_matches_search_name (&(*top)->ginfo,
703 							 lookup_name))
704 	top--;
705 
706       /* Fixup to have a symbol which matches SYMBOL_MATCHES_SEARCH_NAME.  */
707       top++;
708 
709       while (top <= real_top && symbol_matches_search_name (&(*top)->ginfo,
710 							    lookup_name))
711 	{
712 	  if (symbol_matches_domain ((*top)->ginfo.language (),
713 				     (*top)->domain, domain))
714 	    return *top;
715 	  top++;
716 	}
717     }
718 
719   /* Can't use a binary search or else we found during the binary search that
720      we should also do a linear search.  */
721 
722   if (do_linear_search)
723     {
724       for (psym = start; psym < start + length; psym++)
725 	{
726 	  if (symbol_matches_domain ((*psym)->ginfo.language (),
727 				     (*psym)->domain, domain)
728 	      && symbol_matches_search_name (&(*psym)->ginfo, lookup_name))
729 	    return *psym;
730 	}
731     }
732 
733   return NULL;
734 }
735 
736 /* Get the symbol table that corresponds to a partial_symtab.
737    This is fast after the first time you do it.
738    The result will be NULL if the primary symtab has no symbols,
739    which can happen.  Otherwise the result is the primary symtab
740    that contains PST.  */
741 
742 static struct compunit_symtab *
psymtab_to_symtab(struct objfile * objfile,struct partial_symtab * pst)743 psymtab_to_symtab (struct objfile *objfile, struct partial_symtab *pst)
744 {
745   /* If it is a shared psymtab, find an unshared psymtab that includes
746      it.  Any such psymtab will do.  */
747   while (pst->user != NULL)
748     pst = pst->user;
749 
750   /* If it's been looked up before, return it.  */
751   if (pst->get_compunit_symtab (objfile))
752     return pst->get_compunit_symtab (objfile);
753 
754   /* If it has not yet been read in, read it.  */
755   if (!pst->readin_p (objfile))
756     {
757       scoped_restore decrementer = increment_reading_symtab ();
758 
759       if (info_verbose)
760 	{
761 	  printf_filtered (_("Reading in symbols for %s...\n"),
762 			   pst->filename);
763 	  gdb_flush (gdb_stdout);
764 	}
765 
766       pst->read_symtab (objfile);
767     }
768 
769   return pst->get_compunit_symtab (objfile);
770 }
771 
772 /* Psymtab version of find_last_source_symtab.  See its definition in
773    the definition of quick_symbol_functions in symfile.h.  */
774 
775 static struct symtab *
psym_find_last_source_symtab(struct objfile * ofp)776 psym_find_last_source_symtab (struct objfile *ofp)
777 {
778   struct partial_symtab *cs_pst = NULL;
779 
780   for (partial_symtab *ps : require_partial_symbols (ofp, true))
781     {
782       const char *name = ps->filename;
783       int len = strlen (name);
784 
785       if (!(len > 2 && (strcmp (&name[len - 2], ".h") == 0
786 			|| strcmp (name, "<<C++-namespaces>>") == 0)))
787 	cs_pst = ps;
788     }
789 
790   if (cs_pst)
791     {
792       if (cs_pst->readin_p (ofp))
793 	{
794 	  internal_error (__FILE__, __LINE__,
795 			  _("select_source_symtab: "
796 			  "readin pst found and no symtabs."));
797 	}
798       else
799 	{
800 	  struct compunit_symtab *cust = psymtab_to_symtab (ofp, cs_pst);
801 
802 	  if (cust == NULL)
803 	    return NULL;
804 	  return compunit_primary_filetab (cust);
805 	}
806     }
807   return NULL;
808 }
809 
810 /* Psymtab version of forget_cached_source_info.  See its definition in
811    the definition of quick_symbol_functions in symfile.h.  */
812 
813 static void
psym_forget_cached_source_info(struct objfile * objfile)814 psym_forget_cached_source_info (struct objfile *objfile)
815 {
816   for (partial_symtab *pst : require_partial_symbols (objfile, true))
817     {
818       if (pst->fullname != NULL)
819 	{
820 	  xfree (pst->fullname);
821 	  pst->fullname = NULL;
822 	}
823     }
824 }
825 
826 static void
print_partial_symbols(struct gdbarch * gdbarch,struct objfile * objfile,struct partial_symbol ** p,int count,const char * what,struct ui_file * outfile)827 print_partial_symbols (struct gdbarch *gdbarch, struct objfile *objfile,
828 		       struct partial_symbol **p, int count, const char *what,
829 		       struct ui_file *outfile)
830 {
831   fprintf_filtered (outfile, "  %s partial symbols:\n", what);
832   while (count-- > 0)
833     {
834       QUIT;
835       fprintf_filtered (outfile, "    `%s'", (*p)->ginfo.linkage_name ());
836       if ((*p)->ginfo.demangled_name () != NULL)
837 	{
838 	  fprintf_filtered (outfile, "  `%s'",
839 			    (*p)->ginfo.demangled_name ());
840 	}
841       fputs_filtered (", ", outfile);
842       switch ((*p)->domain)
843 	{
844 	case UNDEF_DOMAIN:
845 	  fputs_filtered ("undefined domain, ", outfile);
846 	  break;
847 	case VAR_DOMAIN:
848 	  /* This is the usual thing -- don't print it.  */
849 	  break;
850 	case STRUCT_DOMAIN:
851 	  fputs_filtered ("struct domain, ", outfile);
852 	  break;
853 	case MODULE_DOMAIN:
854 	  fputs_filtered ("module domain, ", outfile);
855 	  break;
856 	case LABEL_DOMAIN:
857 	  fputs_filtered ("label domain, ", outfile);
858 	  break;
859 	case COMMON_BLOCK_DOMAIN:
860 	  fputs_filtered ("common block domain, ", outfile);
861 	  break;
862 	default:
863 	  fputs_filtered ("<invalid domain>, ", outfile);
864 	  break;
865 	}
866       switch ((*p)->aclass)
867 	{
868 	case LOC_UNDEF:
869 	  fputs_filtered ("undefined", outfile);
870 	  break;
871 	case LOC_CONST:
872 	  fputs_filtered ("constant int", outfile);
873 	  break;
874 	case LOC_STATIC:
875 	  fputs_filtered ("static", outfile);
876 	  break;
877 	case LOC_REGISTER:
878 	  fputs_filtered ("register", outfile);
879 	  break;
880 	case LOC_ARG:
881 	  fputs_filtered ("pass by value", outfile);
882 	  break;
883 	case LOC_REF_ARG:
884 	  fputs_filtered ("pass by reference", outfile);
885 	  break;
886 	case LOC_REGPARM_ADDR:
887 	  fputs_filtered ("register address parameter", outfile);
888 	  break;
889 	case LOC_LOCAL:
890 	  fputs_filtered ("stack parameter", outfile);
891 	  break;
892 	case LOC_TYPEDEF:
893 	  fputs_filtered ("type", outfile);
894 	  break;
895 	case LOC_LABEL:
896 	  fputs_filtered ("label", outfile);
897 	  break;
898 	case LOC_BLOCK:
899 	  fputs_filtered ("function", outfile);
900 	  break;
901 	case LOC_CONST_BYTES:
902 	  fputs_filtered ("constant bytes", outfile);
903 	  break;
904 	case LOC_UNRESOLVED:
905 	  fputs_filtered ("unresolved", outfile);
906 	  break;
907 	case LOC_OPTIMIZED_OUT:
908 	  fputs_filtered ("optimized out", outfile);
909 	  break;
910 	case LOC_COMPUTED:
911 	  fputs_filtered ("computed at runtime", outfile);
912 	  break;
913 	default:
914 	  fputs_filtered ("<invalid location>", outfile);
915 	  break;
916 	}
917       fputs_filtered (", ", outfile);
918       fputs_filtered (paddress (gdbarch, (*p)->unrelocated_address ()), outfile);
919       fprintf_filtered (outfile, "\n");
920       p++;
921     }
922 }
923 
924 static void
dump_psymtab(struct objfile * objfile,struct partial_symtab * psymtab,struct ui_file * outfile)925 dump_psymtab (struct objfile *objfile, struct partial_symtab *psymtab,
926 	      struct ui_file *outfile)
927 {
928   struct gdbarch *gdbarch = objfile->arch ();
929   int i;
930 
931   if (psymtab->anonymous)
932     {
933       fprintf_filtered (outfile, "\nAnonymous partial symtab (%s) ",
934 			psymtab->filename);
935     }
936   else
937     {
938       fprintf_filtered (outfile, "\nPartial symtab for source file %s ",
939 			psymtab->filename);
940     }
941   fprintf_filtered (outfile, "(object ");
942   gdb_print_host_address (psymtab, outfile);
943   fprintf_filtered (outfile, ")\n\n");
944   fprintf_filtered (outfile, "  Read from object file %s (",
945 		    objfile_name (objfile));
946   gdb_print_host_address (objfile, outfile);
947   fprintf_filtered (outfile, ")\n");
948 
949   if (psymtab->readin_p (objfile))
950     {
951       fprintf_filtered (outfile,
952 			"  Full symtab was read (at ");
953       gdb_print_host_address (psymtab->get_compunit_symtab (objfile), outfile);
954       fprintf_filtered (outfile, ")\n");
955     }
956 
957   fprintf_filtered (outfile, "  Symbols cover text addresses ");
958   fputs_filtered (paddress (gdbarch, psymtab->text_low (objfile)), outfile);
959   fprintf_filtered (outfile, "-");
960   fputs_filtered (paddress (gdbarch, psymtab->text_high (objfile)), outfile);
961   fprintf_filtered (outfile, "\n");
962   fprintf_filtered (outfile, "  Address map supported - %s.\n",
963 		    psymtab->psymtabs_addrmap_supported ? "yes" : "no");
964   fprintf_filtered (outfile, "  Depends on %d other partial symtabs.\n",
965 		    psymtab->number_of_dependencies);
966   for (i = 0; i < psymtab->number_of_dependencies; i++)
967     {
968       fprintf_filtered (outfile, "    %d ", i);
969       gdb_print_host_address (psymtab->dependencies[i], outfile);
970       fprintf_filtered (outfile, " %s\n",
971 			psymtab->dependencies[i]->filename);
972     }
973   if (psymtab->user != NULL)
974     {
975       fprintf_filtered (outfile, "  Shared partial symtab with user ");
976       gdb_print_host_address (psymtab->user, outfile);
977       fprintf_filtered (outfile, "\n");
978     }
979   if (psymtab->n_global_syms > 0)
980     {
981       print_partial_symbols
982 	(gdbarch, objfile,
983 	 &objfile->partial_symtabs->global_psymbols[psymtab->globals_offset],
984 	 psymtab->n_global_syms, "Global", outfile);
985     }
986   if (psymtab->n_static_syms > 0)
987     {
988       print_partial_symbols
989 	(gdbarch, objfile,
990 	 &objfile->partial_symtabs->static_psymbols[psymtab->statics_offset],
991 	 psymtab->n_static_syms, "Static", outfile);
992     }
993   fprintf_filtered (outfile, "\n");
994 }
995 
996 /* Psymtab version of print_stats.  See its definition in
997    the definition of quick_symbol_functions in symfile.h.  */
998 
999 static void
psym_print_stats(struct objfile * objfile)1000 psym_print_stats (struct objfile *objfile)
1001 {
1002   int i;
1003 
1004   i = 0;
1005   for (partial_symtab *ps : require_partial_symbols (objfile, true))
1006     {
1007       if (!ps->readin_p (objfile))
1008 	i++;
1009     }
1010   printf_filtered (_("  Number of psym tables (not yet expanded): %d\n"), i);
1011 }
1012 
1013 /* Psymtab version of dump.  See its definition in
1014    the definition of quick_symbol_functions in symfile.h.  */
1015 
1016 static void
psym_dump(struct objfile * objfile)1017 psym_dump (struct objfile *objfile)
1018 {
1019   struct partial_symtab *psymtab;
1020 
1021   if (objfile->partial_symtabs->psymtabs)
1022     {
1023       printf_filtered ("Psymtabs:\n");
1024       for (psymtab = objfile->partial_symtabs->psymtabs;
1025 	   psymtab != NULL;
1026 	   psymtab = psymtab->next)
1027 	{
1028 	  printf_filtered ("%s at ",
1029 			   psymtab->filename);
1030 	  gdb_print_host_address (psymtab, gdb_stdout);
1031 	  printf_filtered (", ");
1032 	  wrap_here ("  ");
1033 	}
1034       printf_filtered ("\n\n");
1035     }
1036 }
1037 
1038 /* Psymtab version of expand_symtabs_for_function.  See its definition in
1039    the definition of quick_symbol_functions in symfile.h.  */
1040 
1041 static void
psym_expand_symtabs_for_function(struct objfile * objfile,const char * func_name)1042 psym_expand_symtabs_for_function (struct objfile *objfile,
1043 				  const char *func_name)
1044 {
1045   lookup_name_info base_lookup (func_name, symbol_name_match_type::FULL);
1046   lookup_name_info lookup_name = base_lookup.make_ignore_params ();
1047 
1048   for (partial_symtab *ps : require_partial_symbols (objfile, true))
1049     {
1050       if (ps->readin_p (objfile))
1051 	continue;
1052 
1053       if ((lookup_partial_symbol (objfile, ps, lookup_name, 1, VAR_DOMAIN)
1054 	   != NULL)
1055 	  || (lookup_partial_symbol (objfile, ps, lookup_name, 0, VAR_DOMAIN)
1056 	      != NULL))
1057 	psymtab_to_symtab (objfile, ps);
1058     }
1059 }
1060 
1061 /* Psymtab version of expand_all_symtabs.  See its definition in
1062    the definition of quick_symbol_functions in symfile.h.  */
1063 
1064 static void
psym_expand_all_symtabs(struct objfile * objfile)1065 psym_expand_all_symtabs (struct objfile *objfile)
1066 {
1067   for (partial_symtab *psymtab : require_partial_symbols (objfile, true))
1068     psymtab_to_symtab (objfile, psymtab);
1069 }
1070 
1071 /* Psymtab version of expand_symtabs_with_fullname.  See its definition in
1072    the definition of quick_symbol_functions in symfile.h.  */
1073 
1074 static void
psym_expand_symtabs_with_fullname(struct objfile * objfile,const char * fullname)1075 psym_expand_symtabs_with_fullname (struct objfile *objfile,
1076 				   const char *fullname)
1077 {
1078   for (partial_symtab *p : require_partial_symbols (objfile, true))
1079     {
1080       /* Anonymous psymtabs don't have a name of a source file.  */
1081       if (p->anonymous)
1082 	continue;
1083 
1084       /* psymtab_to_fullname tries to open the file which is slow.
1085 	 Don't call it if we know the basenames don't match.  */
1086       if ((basenames_may_differ
1087 	   || filename_cmp (lbasename (fullname), lbasename (p->filename)) == 0)
1088 	  && filename_cmp (fullname, psymtab_to_fullname (p)) == 0)
1089 	psymtab_to_symtab (objfile, p);
1090     }
1091 }
1092 
1093 /* Psymtab version of map_symbol_filenames.  See its definition in
1094    the definition of quick_symbol_functions in symfile.h.  */
1095 
1096 static void
psym_map_symbol_filenames(struct objfile * objfile,symbol_filename_ftype * fun,void * data,int need_fullname)1097 psym_map_symbol_filenames (struct objfile *objfile,
1098 			   symbol_filename_ftype *fun, void *data,
1099 			   int need_fullname)
1100 {
1101   for (partial_symtab *ps : require_partial_symbols (objfile, true))
1102     {
1103       const char *fullname;
1104 
1105       if (ps->readin_p (objfile))
1106 	continue;
1107 
1108       /* We can skip shared psymtabs here, because any file name will be
1109 	 attached to the unshared psymtab.  */
1110       if (ps->user != NULL)
1111 	continue;
1112 
1113       /* Anonymous psymtabs don't have a file name.  */
1114       if (ps->anonymous)
1115 	continue;
1116 
1117       QUIT;
1118       if (need_fullname)
1119 	fullname = psymtab_to_fullname (ps);
1120       else
1121 	fullname = NULL;
1122       (*fun) (ps->filename, fullname, data);
1123     }
1124 }
1125 
1126 /* Finds the fullname that a partial_symtab represents.
1127 
1128    If this functions finds the fullname, it will save it in ps->fullname
1129    and it will also return the value.
1130 
1131    If this function fails to find the file that this partial_symtab represents,
1132    NULL will be returned and ps->fullname will be set to NULL.  */
1133 
1134 static const char *
psymtab_to_fullname(struct partial_symtab * ps)1135 psymtab_to_fullname (struct partial_symtab *ps)
1136 {
1137   gdb_assert (!ps->anonymous);
1138 
1139   /* Use cached copy if we have it.
1140      We rely on forget_cached_source_info being called appropriately
1141      to handle cases like the file being moved.  */
1142   if (ps->fullname == NULL)
1143     {
1144       gdb::unique_xmalloc_ptr<char> fullname;
1145       scoped_fd fd = find_and_open_source (ps->filename, ps->dirname,
1146 					   &fullname);
1147       ps->fullname = fullname.release ();
1148 
1149       if (fd.get () < 0)
1150 	{
1151 	  /* rewrite_source_path would be applied by find_and_open_source, we
1152 	     should report the pathname where GDB tried to find the file.  */
1153 
1154 	  if (ps->dirname == NULL || IS_ABSOLUTE_PATH (ps->filename))
1155 	    fullname.reset (xstrdup (ps->filename));
1156 	  else
1157 	    fullname.reset (concat (ps->dirname, SLASH_STRING,
1158 				    ps->filename, (char *) NULL));
1159 
1160 	  ps->fullname = rewrite_source_path (fullname.get ()).release ();
1161 	  if (ps->fullname == NULL)
1162 	    ps->fullname = fullname.release ();
1163 	}
1164     }
1165 
1166   return ps->fullname;
1167 }
1168 
1169 /* Psymtab version of map_matching_symbols.  See its definition in
1170    the definition of quick_symbol_functions in symfile.h.  */
1171 
1172 static void
psym_map_matching_symbols(struct objfile * objfile,const lookup_name_info & name,domain_enum domain,int global,gdb::function_view<symbol_found_callback_ftype> callback,symbol_compare_ftype * ordered_compare)1173 psym_map_matching_symbols
1174   (struct objfile *objfile,
1175    const lookup_name_info &name, domain_enum domain,
1176    int global,
1177    gdb::function_view<symbol_found_callback_ftype> callback,
1178    symbol_compare_ftype *ordered_compare)
1179 {
1180   const int block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
1181 
1182   for (partial_symtab *ps : require_partial_symbols (objfile, true))
1183     {
1184       QUIT;
1185       if (ps->readin_p (objfile)
1186 	  || match_partial_symbol (objfile, ps, global, name, domain,
1187 				   ordered_compare))
1188 	{
1189 	  struct compunit_symtab *cust = psymtab_to_symtab (objfile, ps);
1190 	  const struct block *block;
1191 
1192 	  if (cust == NULL)
1193 	    continue;
1194 	  block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
1195 	  if (!iterate_over_symbols_terminated (block, name,
1196 						domain, callback))
1197 	    return;
1198 	}
1199     }
1200 }
1201 
1202 /* A helper for psym_expand_symtabs_matching that handles searching
1203    included psymtabs.  This returns true if a symbol is found, and
1204    false otherwise.  It also updates the 'searched_flag' on the
1205    various psymtabs that it searches.  */
1206 
1207 static bool
recursively_search_psymtabs(struct partial_symtab * ps,struct objfile * objfile,enum search_domain domain,const lookup_name_info & lookup_name,gdb::function_view<expand_symtabs_symbol_matcher_ftype> sym_matcher)1208 recursively_search_psymtabs
1209   (struct partial_symtab *ps,
1210    struct objfile *objfile,
1211    enum search_domain domain,
1212    const lookup_name_info &lookup_name,
1213    gdb::function_view<expand_symtabs_symbol_matcher_ftype> sym_matcher)
1214 {
1215   int keep_going = 1;
1216   enum psymtab_search_status result = PST_SEARCHED_AND_NOT_FOUND;
1217   int i;
1218 
1219   if (ps->searched_flag != PST_NOT_SEARCHED)
1220     return ps->searched_flag == PST_SEARCHED_AND_FOUND;
1221 
1222   /* Recurse into shared psymtabs first, because they may have already
1223      been searched, and this could save some time.  */
1224   for (i = 0; i < ps->number_of_dependencies; ++i)
1225     {
1226       int r;
1227 
1228       /* Skip non-shared dependencies, these are handled elsewhere.  */
1229       if (ps->dependencies[i]->user == NULL)
1230 	continue;
1231 
1232       r = recursively_search_psymtabs (ps->dependencies[i],
1233 				       objfile, domain, lookup_name,
1234 				       sym_matcher);
1235       if (r != 0)
1236 	{
1237 	  ps->searched_flag = PST_SEARCHED_AND_FOUND;
1238 	  return true;
1239 	}
1240     }
1241 
1242   partial_symbol **gbound
1243     = (objfile->partial_symtabs->global_psymbols.data ()
1244        + ps->globals_offset + ps->n_global_syms);
1245   partial_symbol **sbound
1246     = (objfile->partial_symtabs->static_psymbols.data ()
1247        + ps->statics_offset + ps->n_static_syms);
1248   partial_symbol **bound = gbound;
1249 
1250   /* Go through all of the symbols stored in a partial
1251      symtab in one loop.  */
1252   partial_symbol **psym = (objfile->partial_symtabs->global_psymbols.data ()
1253 			   + ps->globals_offset);
1254   while (keep_going)
1255     {
1256       if (psym >= bound)
1257 	{
1258 	  if (bound == gbound && ps->n_static_syms != 0)
1259 	    {
1260 	      psym = (objfile->partial_symtabs->static_psymbols.data ()
1261 		      + ps->statics_offset);
1262 	      bound = sbound;
1263 	    }
1264 	  else
1265 	    keep_going = 0;
1266 	  continue;
1267 	}
1268       else
1269 	{
1270 	  QUIT;
1271 
1272 	  if ((domain == ALL_DOMAIN
1273 	       || (domain == MODULES_DOMAIN
1274 		   && (*psym)->domain == MODULE_DOMAIN)
1275 	       || (domain == VARIABLES_DOMAIN
1276 		   && (*psym)->aclass != LOC_TYPEDEF
1277 		   && (*psym)->aclass != LOC_BLOCK)
1278 	       || (domain == FUNCTIONS_DOMAIN
1279 		   && (*psym)->aclass == LOC_BLOCK)
1280 	       || (domain == TYPES_DOMAIN
1281 		   && (*psym)->aclass == LOC_TYPEDEF))
1282 	      && psymbol_name_matches (*psym, lookup_name)
1283 	      && (sym_matcher == NULL
1284 		  || sym_matcher ((*psym)->ginfo.search_name ())))
1285 	    {
1286 	      /* Found a match, so notify our caller.  */
1287 	      result = PST_SEARCHED_AND_FOUND;
1288 	      keep_going = 0;
1289 	    }
1290 	}
1291       psym++;
1292     }
1293 
1294   ps->searched_flag = result;
1295   return result == PST_SEARCHED_AND_FOUND;
1296 }
1297 
1298 /* Psymtab version of expand_symtabs_matching.  See its definition in
1299    the definition of quick_symbol_functions in symfile.h.  */
1300 
1301 static void
psym_expand_symtabs_matching(struct objfile * objfile,gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,const lookup_name_info * lookup_name,gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,enum search_domain domain)1302 psym_expand_symtabs_matching
1303   (struct objfile *objfile,
1304    gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
1305    const lookup_name_info *lookup_name,
1306    gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
1307    gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
1308    enum search_domain domain)
1309 {
1310   /* Clear the search flags.  */
1311   for (partial_symtab *ps : require_partial_symbols (objfile, true))
1312     ps->searched_flag = PST_NOT_SEARCHED;
1313 
1314   for (partial_symtab *ps : objfile->psymtabs ())
1315     {
1316       QUIT;
1317 
1318       if (ps->readin_p (objfile))
1319 	continue;
1320 
1321       /* We skip shared psymtabs because file-matching doesn't apply
1322 	 to them; but we search them later in the loop.  */
1323       if (ps->user != NULL)
1324 	continue;
1325 
1326       if (file_matcher)
1327 	{
1328 	  bool match;
1329 
1330 	  if (ps->anonymous)
1331 	    continue;
1332 
1333 	  match = file_matcher (ps->filename, false);
1334 	  if (!match)
1335 	    {
1336 	      /* Before we invoke realpath, which can get expensive when many
1337 		 files are involved, do a quick comparison of the basenames.  */
1338 	      if (basenames_may_differ
1339 		  || file_matcher (lbasename (ps->filename), true))
1340 		match = file_matcher (psymtab_to_fullname (ps), false);
1341 	    }
1342 	  if (!match)
1343 	    continue;
1344 	}
1345 
1346       if ((symbol_matcher == NULL && lookup_name == NULL)
1347 	  || recursively_search_psymtabs (ps, objfile, domain,
1348 					  lookup_name->make_ignore_params (),
1349 					  symbol_matcher))
1350 	{
1351 	  struct compunit_symtab *symtab =
1352 	    psymtab_to_symtab (objfile, ps);
1353 
1354 	  if (expansion_notify != NULL)
1355 	    expansion_notify (symtab);
1356 	}
1357     }
1358 }
1359 
1360 /* Psymtab version of has_symbols.  See its definition in
1361    the definition of quick_symbol_functions in symfile.h.  */
1362 
1363 static int
psym_has_symbols(struct objfile * objfile)1364 psym_has_symbols (struct objfile *objfile)
1365 {
1366   return objfile->partial_symtabs->psymtabs != NULL;
1367 }
1368 
1369 /* Helper function for psym_find_compunit_symtab_by_address that fills
1370    in psymbol_map for a given range of psymbols.  */
1371 
1372 static void
psym_fill_psymbol_map(struct objfile * objfile,struct partial_symtab * psymtab,std::set<CORE_ADDR> * seen_addrs,const std::vector<partial_symbol * > & symbols,int start,int length)1373 psym_fill_psymbol_map (struct objfile *objfile,
1374 		       struct partial_symtab *psymtab,
1375 		       std::set<CORE_ADDR> *seen_addrs,
1376 		       const std::vector<partial_symbol *> &symbols,
1377 		       int start,
1378 		       int length)
1379 {
1380   for (int i = 0; i < length; ++i)
1381     {
1382       struct partial_symbol *psym = symbols[start + i];
1383 
1384       if (psym->aclass == LOC_STATIC)
1385 	{
1386 	  CORE_ADDR addr = psym->address (objfile);
1387 	  if (seen_addrs->find (addr) == seen_addrs->end ())
1388 	    {
1389 	      seen_addrs->insert (addr);
1390 	      objfile->psymbol_map.emplace_back (addr, psymtab);
1391 	    }
1392 	}
1393     }
1394 }
1395 
1396 /* See find_compunit_symtab_by_address in quick_symbol_functions, in
1397    symfile.h.  */
1398 
1399 static compunit_symtab *
psym_find_compunit_symtab_by_address(struct objfile * objfile,CORE_ADDR address)1400 psym_find_compunit_symtab_by_address (struct objfile *objfile,
1401 				      CORE_ADDR address)
1402 {
1403   if (objfile->psymbol_map.empty ())
1404     {
1405       std::set<CORE_ADDR> seen_addrs;
1406 
1407       for (partial_symtab *pst : require_partial_symbols (objfile, true))
1408 	{
1409 	  psym_fill_psymbol_map (objfile, pst,
1410 				 &seen_addrs,
1411 				 objfile->partial_symtabs->global_psymbols,
1412 				 pst->globals_offset,
1413 				 pst->n_global_syms);
1414 	  psym_fill_psymbol_map (objfile, pst,
1415 				 &seen_addrs,
1416 				 objfile->partial_symtabs->static_psymbols,
1417 				 pst->statics_offset,
1418 				 pst->n_static_syms);
1419 	}
1420 
1421       objfile->psymbol_map.shrink_to_fit ();
1422 
1423       std::sort (objfile->psymbol_map.begin (), objfile->psymbol_map.end (),
1424 		 [] (const std::pair<CORE_ADDR, partial_symtab *> &a,
1425 		     const std::pair<CORE_ADDR, partial_symtab *> &b)
1426 		 {
1427 		   return a.first < b.first;
1428 		 });
1429     }
1430 
1431   auto iter = std::lower_bound
1432     (objfile->psymbol_map.begin (), objfile->psymbol_map.end (), address,
1433      [] (const std::pair<CORE_ADDR, partial_symtab *> &a,
1434 	 CORE_ADDR b)
1435      {
1436        return a.first < b;
1437      });
1438 
1439   if (iter == objfile->psymbol_map.end () || iter->first != address)
1440     return NULL;
1441 
1442   return psymtab_to_symtab (objfile, iter->second);
1443 }
1444 
1445 const struct quick_symbol_functions psym_functions =
1446 {
1447   psym_has_symbols,
1448   psym_find_last_source_symtab,
1449   psym_forget_cached_source_info,
1450   psym_map_symtabs_matching_filename,
1451   psym_lookup_symbol,
1452   psym_lookup_global_symbol_language,
1453   psym_print_stats,
1454   psym_dump,
1455   psym_expand_symtabs_for_function,
1456   psym_expand_all_symtabs,
1457   psym_expand_symtabs_with_fullname,
1458   psym_map_matching_symbols,
1459   psym_expand_symtabs_matching,
1460   psym_find_pc_sect_compunit_symtab,
1461   psym_find_compunit_symtab_by_address,
1462   psym_map_symbol_filenames
1463 };
1464 
1465 
1466 
1467 static void
sort_pst_symbols(struct objfile * objfile,struct partial_symtab * pst)1468 sort_pst_symbols (struct objfile *objfile, struct partial_symtab *pst)
1469 {
1470   /* Sort the global list; don't sort the static list.  */
1471   auto begin = objfile->partial_symtabs->global_psymbols.begin ();
1472   std::advance (begin, pst->globals_offset);
1473 
1474   /* The psymbols for this partial_symtab are currently at the end of the
1475      vector.  */
1476   auto end = objfile->partial_symtabs->global_psymbols.end ();
1477 
1478   std::sort (begin, end, [] (partial_symbol *s1, partial_symbol *s2)
1479     {
1480       return strcmp_iw_ordered (s1->ginfo.search_name (),
1481 				s2->ginfo.search_name ()) < 0;
1482     });
1483 }
1484 
1485 /* Partially fill a partial symtab.  It will be completely filled at
1486    the end of the symbol list.  */
1487 
partial_symtab(const char * filename,struct objfile * objfile,CORE_ADDR textlow)1488 partial_symtab::partial_symtab (const char *filename,
1489 				struct objfile *objfile,
1490 				CORE_ADDR textlow)
1491   : partial_symtab (filename, objfile)
1492 {
1493   set_text_low (textlow);
1494   set_text_high (raw_text_low ()); /* default */
1495 
1496   auto *v1 = new std::vector<partial_symbol *>;
1497   objfile->partial_symtabs->current_global_psymbols.push_back (v1);
1498   auto *v2 = new std::vector<partial_symbol *>;
1499   objfile->partial_symtabs->current_static_psymbols.push_back (v2);
1500 }
1501 
1502 /* Concat vectors V1 and V2.  */
1503 
1504 static void
concat(std::vector<partial_symbol * > * v1,std::vector<partial_symbol * > * v2)1505 concat (std::vector<partial_symbol *> *v1, std::vector<partial_symbol *> *v2)
1506 {
1507   v1->insert (v1->end (), v2->begin (), v2->end ());
1508   v2->clear ();
1509 }
1510 
1511 /* Perform "finishing up" operations of a partial symtab.  */
1512 
1513 void
end_psymtab_common(struct objfile * objfile,struct partial_symtab * pst)1514 end_psymtab_common (struct objfile *objfile, struct partial_symtab *pst)
1515 {
1516   pst->globals_offset = objfile->partial_symtabs->global_psymbols.size ();
1517   pst->statics_offset = objfile->partial_symtabs->static_psymbols.size ();
1518 
1519   auto *current_global_psymbols
1520     = objfile->partial_symtabs->current_global_psymbols.back ();
1521   auto *current_static_psymbols
1522     = objfile->partial_symtabs->current_static_psymbols.back ();
1523   objfile->partial_symtabs->current_global_psymbols.pop_back ();
1524   objfile->partial_symtabs->current_static_psymbols.pop_back ();
1525 
1526   pst->n_global_syms
1527     = current_global_psymbols->size ();
1528   pst->n_static_syms
1529     = current_static_psymbols->size ();
1530 
1531   concat (&objfile->partial_symtabs->global_psymbols, current_global_psymbols);
1532   concat (&objfile->partial_symtabs->static_psymbols, current_static_psymbols);
1533 
1534   delete current_global_psymbols;
1535   delete current_static_psymbols;
1536 
1537   sort_pst_symbols (objfile, pst);
1538 }
1539 
1540 /* Calculate a hash code for the given partial symbol.  The hash is
1541    calculated using the symbol's value, language, domain, class
1542    and name.  These are the values which are set by
1543    add_psymbol_to_bcache.  */
1544 
1545 static unsigned long
psymbol_hash(const void * addr,int length)1546 psymbol_hash (const void *addr, int length)
1547 {
1548   unsigned long h = 0;
1549   struct partial_symbol *psymbol = (struct partial_symbol *) addr;
1550   unsigned int lang = psymbol->ginfo.language ();
1551   unsigned int domain = psymbol->domain;
1552   unsigned int theclass = psymbol->aclass;
1553 
1554   h = fast_hash (&psymbol->ginfo.value, sizeof (psymbol->ginfo.value), h);
1555   h = fast_hash (&lang, sizeof (unsigned int), h);
1556   h = fast_hash (&domain, sizeof (unsigned int), h);
1557   h = fast_hash (&theclass, sizeof (unsigned int), h);
1558   /* Note that psymbol names are interned via compute_and_set_names, so
1559      there's no need to hash the contents of the name here.  */
1560   h = fast_hash (&psymbol->ginfo.m_name, sizeof (psymbol->ginfo.m_name), h);
1561 
1562   return h;
1563 }
1564 
1565 /* Returns true if the symbol at addr1 equals the symbol at addr2.
1566    For the comparison this function uses a symbols value,
1567    language, domain, class and name.  */
1568 
1569 static int
psymbol_compare(const void * addr1,const void * addr2,int length)1570 psymbol_compare (const void *addr1, const void *addr2, int length)
1571 {
1572   struct partial_symbol *sym1 = (struct partial_symbol *) addr1;
1573   struct partial_symbol *sym2 = (struct partial_symbol *) addr2;
1574 
1575   return (memcmp (&sym1->ginfo.value, &sym2->ginfo.value,
1576                   sizeof (sym1->ginfo.value)) == 0
1577 	  && sym1->ginfo.language () == sym2->ginfo.language ()
1578           && sym1->domain == sym2->domain
1579           && sym1->aclass == sym2->aclass
1580 	  /* Note that psymbol names are interned via
1581 	     compute_and_set_names, so there's no need to compare the
1582 	     contents of the name here.  */
1583           && sym1->ginfo.linkage_name () == sym2->ginfo.linkage_name ());
1584 }
1585 
1586 /* Helper function, initialises partial symbol structure and stashes
1587    it into objfile's bcache.  Note that our caching mechanism will
1588    use all fields of struct partial_symbol to determine hash value of the
1589    structure.  In other words, having two symbols with the same name but
1590    different domain (or address) is possible and correct.  */
1591 
1592 static struct partial_symbol *
add_psymbol_to_bcache(const partial_symbol & psymbol,struct objfile * objfile,bool * added)1593 add_psymbol_to_bcache (const partial_symbol &psymbol, struct objfile *objfile,
1594 		       bool *added)
1595 {
1596   /* Stash the partial symbol away in the cache.  */
1597   return ((struct partial_symbol *)
1598 	  objfile->partial_symtabs->psymbol_cache.insert
1599 	  (&psymbol, sizeof (struct partial_symbol), added));
1600 }
1601 
1602 /* Helper function, adds partial symbol to the given partial symbol list.  */
1603 
1604 static void
append_psymbol_to_list(std::vector<partial_symbol * > * list,struct partial_symbol * psym,struct objfile * objfile)1605 append_psymbol_to_list (std::vector<partial_symbol *> *list,
1606 			struct partial_symbol *psym,
1607 			struct objfile *objfile)
1608 {
1609   list->push_back (psym);
1610   OBJSTAT (objfile, n_psyms++);
1611 }
1612 
1613 /* See psympriv.h.  */
1614 
1615 void
add_psymbol_to_list(const partial_symbol & psymbol,psymbol_placement where,struct objfile * objfile)1616 add_psymbol_to_list (const partial_symbol &psymbol,
1617 		     psymbol_placement where,
1618 		     struct objfile *objfile)
1619 {
1620   bool added;
1621 
1622   /* Stash the partial symbol away in the cache.  */
1623   partial_symbol *psym = add_psymbol_to_bcache (psymbol, objfile, &added);
1624 
1625   /* Do not duplicate global partial symbols.  */
1626   if (where == psymbol_placement::GLOBAL && !added)
1627     return;
1628 
1629   /* Save pointer to partial symbol in psymtab, growing symtab if needed.  */
1630   std::vector<partial_symbol *> *list
1631     = (where == psymbol_placement::STATIC
1632        ? objfile->partial_symtabs->current_static_psymbols.back ()
1633        : objfile->partial_symtabs->current_global_psymbols.back ());
1634   append_psymbol_to_list (list, psym, objfile);
1635 }
1636 
1637 /* See psympriv.h.  */
1638 
1639 void
add_psymbol_to_list(gdb::string_view name,bool copy_name,domain_enum domain,enum address_class theclass,short section,psymbol_placement where,CORE_ADDR coreaddr,enum language language,struct objfile * objfile)1640 add_psymbol_to_list (gdb::string_view name, bool copy_name,
1641 		     domain_enum domain,
1642 		     enum address_class theclass,
1643 		     short section,
1644 		     psymbol_placement where,
1645 		     CORE_ADDR coreaddr,
1646 		     enum language language, struct objfile *objfile)
1647 {
1648   struct partial_symbol psymbol;
1649   memset (&psymbol, 0, sizeof (psymbol));
1650 
1651   psymbol.set_unrelocated_address (coreaddr);
1652   psymbol.ginfo.section = section;
1653   psymbol.domain = domain;
1654   psymbol.aclass = theclass;
1655   psymbol.ginfo.set_language (language, objfile->partial_symtabs->obstack ());
1656   psymbol.ginfo.compute_and_set_names (name, copy_name, objfile->per_bfd);
1657 
1658   add_psymbol_to_list (psymbol, where, objfile);
1659 }
1660 
1661 /* See psympriv.h.  */
1662 
1663 void
init_psymbol_list(struct objfile * objfile,int total_symbols)1664 init_psymbol_list (struct objfile *objfile, int total_symbols)
1665 {
1666   if (objfile->partial_symtabs->global_psymbols.capacity () == 0
1667       && objfile->partial_symtabs->static_psymbols.capacity () == 0)
1668     {
1669       /* Current best guess is that approximately a twentieth of the
1670 	 total symbols (in a debugging file) are global or static
1671 	 oriented symbols, then multiply that by slop factor of
1672 	 two.  */
1673       objfile->partial_symtabs->global_psymbols.reserve (total_symbols / 10);
1674       objfile->partial_symtabs->static_psymbols.reserve (total_symbols / 10);
1675     }
1676 }
1677 
1678 /* See psympriv.h.  */
1679 
partial_symtab(const char * filename_,struct objfile * objfile)1680 partial_symtab::partial_symtab (const char *filename_, struct objfile *objfile)
1681   : searched_flag (PST_NOT_SEARCHED),
1682     text_low_valid (0),
1683     text_high_valid (0)
1684 {
1685   objfile->partial_symtabs->install_psymtab (this);
1686 
1687   filename = objfile->intern (filename_);
1688 
1689   if (symtab_create_debug)
1690     {
1691       /* Be a bit clever with debugging messages, and don't print objfile
1692 	 every time, only when it changes.  */
1693       static char *last_objfile_name = NULL;
1694 
1695       if (last_objfile_name == NULL
1696 	  || strcmp (last_objfile_name, objfile_name (objfile)) != 0)
1697 	{
1698 	  xfree (last_objfile_name);
1699 	  last_objfile_name = xstrdup (objfile_name (objfile));
1700 	  fprintf_filtered (gdb_stdlog,
1701 			    "Creating one or more psymtabs for objfile %s ...\n",
1702 			    last_objfile_name);
1703 	}
1704       fprintf_filtered (gdb_stdlog,
1705 			"Created psymtab %s for module %s.\n",
1706 			host_address_to_string (this), filename);
1707     }
1708 }
1709 
1710 /* See psympriv.h.  */
1711 
1712 void
expand_dependencies(struct objfile * objfile)1713 partial_symtab::expand_dependencies (struct objfile *objfile)
1714 {
1715   for (int i = 0; i < number_of_dependencies; ++i)
1716     {
1717       if (!dependencies[i]->readin_p (objfile)
1718 	  && dependencies[i]->user == NULL)
1719 	{
1720 	  /* Inform about additional files to be read in.  */
1721 	  if (info_verbose)
1722 	    {
1723 	      fputs_filtered (" ", gdb_stdout);
1724 	      wrap_here ("");
1725 	      fputs_filtered ("and ", gdb_stdout);
1726 	      wrap_here ("");
1727 	      printf_filtered ("%s...", dependencies[i]->filename);
1728 	      wrap_here ("");	/* Flush output */
1729 	      gdb_flush (gdb_stdout);
1730 	    }
1731 	  dependencies[i]->expand_psymtab (objfile);
1732 	}
1733     }
1734 }
1735 
1736 
1737 void
discard_psymtab(struct partial_symtab * pst)1738 psymtab_storage::discard_psymtab (struct partial_symtab *pst)
1739 {
1740   struct partial_symtab **prev_pst;
1741 
1742   /* From dbxread.c:
1743      Empty psymtabs happen as a result of header files which don't
1744      have any symbols in them.  There can be a lot of them.  But this
1745      check is wrong, in that a psymtab with N_SLINE entries but
1746      nothing else is not empty, but we don't realize that.  Fixing
1747      that without slowing things down might be tricky.  */
1748 
1749   /* First, snip it out of the psymtab chain.  */
1750 
1751   prev_pst = &psymtabs;
1752   while ((*prev_pst) != pst)
1753     prev_pst = &((*prev_pst)->next);
1754   (*prev_pst) = pst->next;
1755   delete pst;
1756 }
1757 
1758 
1759 
1760 /* We need to pass a couple of items to the addrmap_foreach function,
1761    so use a struct.  */
1762 
1763 struct dump_psymtab_addrmap_data
1764 {
1765   struct objfile *objfile;
1766   struct partial_symtab *psymtab;
1767   struct ui_file *outfile;
1768 
1769   /* Non-zero if the previously printed addrmap entry was for PSYMTAB.
1770      If so, we want to print the next one as well (since the next addrmap
1771      entry defines the end of the range).  */
1772   int previous_matched;
1773 };
1774 
1775 /* Helper function for dump_psymtab_addrmap to print an addrmap entry.  */
1776 
1777 static int
dump_psymtab_addrmap_1(void * datap,CORE_ADDR start_addr,void * obj)1778 dump_psymtab_addrmap_1 (void *datap, CORE_ADDR start_addr, void *obj)
1779 {
1780   struct dump_psymtab_addrmap_data *data
1781     = (struct dump_psymtab_addrmap_data *) datap;
1782   struct gdbarch *gdbarch = data->objfile->arch ();
1783   struct partial_symtab *addrmap_psymtab = (struct partial_symtab *) obj;
1784   const char *psymtab_address_or_end = NULL;
1785 
1786   QUIT;
1787 
1788   if (data->psymtab == NULL
1789       || data->psymtab == addrmap_psymtab)
1790     psymtab_address_or_end = host_address_to_string (addrmap_psymtab);
1791   else if (data->previous_matched)
1792     psymtab_address_or_end = "<ends here>";
1793 
1794   if (data->psymtab == NULL
1795       || data->psymtab == addrmap_psymtab
1796       || data->previous_matched)
1797     {
1798       fprintf_filtered (data->outfile, "  %s%s %s\n",
1799 			data->psymtab != NULL ? "  " : "",
1800 			paddress (gdbarch, start_addr),
1801 			psymtab_address_or_end);
1802     }
1803 
1804   data->previous_matched = (data->psymtab == NULL
1805 			    || data->psymtab == addrmap_psymtab);
1806 
1807   return 0;
1808 }
1809 
1810 /* Helper function for maintenance_print_psymbols to print the addrmap
1811    of PSYMTAB.  If PSYMTAB is NULL print the entire addrmap.  */
1812 
1813 static void
dump_psymtab_addrmap(struct objfile * objfile,struct partial_symtab * psymtab,struct ui_file * outfile)1814 dump_psymtab_addrmap (struct objfile *objfile, struct partial_symtab *psymtab,
1815 		      struct ui_file *outfile)
1816 {
1817   struct dump_psymtab_addrmap_data addrmap_dump_data;
1818 
1819   if ((psymtab == NULL
1820        || psymtab->psymtabs_addrmap_supported)
1821       && objfile->partial_symtabs->psymtabs_addrmap != NULL)
1822     {
1823       addrmap_dump_data.objfile = objfile;
1824       addrmap_dump_data.psymtab = psymtab;
1825       addrmap_dump_data.outfile = outfile;
1826       addrmap_dump_data.previous_matched = 0;
1827       fprintf_filtered (outfile, "%sddress map:\n",
1828 			psymtab == NULL ? "Entire a" : "  A");
1829       addrmap_foreach (objfile->partial_symtabs->psymtabs_addrmap,
1830 		       dump_psymtab_addrmap_1, &addrmap_dump_data);
1831     }
1832 }
1833 
1834 static void
maintenance_print_psymbols(const char * args,int from_tty)1835 maintenance_print_psymbols (const char *args, int from_tty)
1836 {
1837   struct ui_file *outfile = gdb_stdout;
1838   char *address_arg = NULL, *source_arg = NULL, *objfile_arg = NULL;
1839   int i, outfile_idx, found;
1840   CORE_ADDR pc = 0;
1841   struct obj_section *section = NULL;
1842 
1843   dont_repeat ();
1844 
1845   gdb_argv argv (args);
1846 
1847   for (i = 0; argv != NULL && argv[i] != NULL; ++i)
1848     {
1849       if (strcmp (argv[i], "-pc") == 0)
1850 	{
1851 	  if (argv[i + 1] == NULL)
1852 	    error (_("Missing pc value"));
1853 	  address_arg = argv[++i];
1854 	}
1855       else if (strcmp (argv[i], "-source") == 0)
1856 	{
1857 	  if (argv[i + 1] == NULL)
1858 	    error (_("Missing source file"));
1859 	  source_arg = argv[++i];
1860 	}
1861       else if (strcmp (argv[i], "-objfile") == 0)
1862 	{
1863 	  if (argv[i + 1] == NULL)
1864 	    error (_("Missing objfile name"));
1865 	  objfile_arg = argv[++i];
1866 	}
1867       else if (strcmp (argv[i], "--") == 0)
1868 	{
1869 	  /* End of options.  */
1870 	  ++i;
1871 	  break;
1872 	}
1873       else if (argv[i][0] == '-')
1874 	{
1875 	  /* Future proofing: Don't allow OUTFILE to begin with "-".  */
1876 	  error (_("Unknown option: %s"), argv[i]);
1877 	}
1878       else
1879 	break;
1880     }
1881   outfile_idx = i;
1882 
1883   if (address_arg != NULL && source_arg != NULL)
1884     error (_("Must specify at most one of -pc and -source"));
1885 
1886   stdio_file arg_outfile;
1887 
1888   if (argv != NULL && argv[outfile_idx] != NULL)
1889     {
1890       if (argv[outfile_idx + 1] != NULL)
1891 	error (_("Junk at end of command"));
1892       gdb::unique_xmalloc_ptr<char> outfile_name
1893 	(tilde_expand (argv[outfile_idx]));
1894       if (!arg_outfile.open (outfile_name.get (), FOPEN_WT))
1895 	perror_with_name (outfile_name.get ());
1896       outfile = &arg_outfile;
1897     }
1898 
1899   if (address_arg != NULL)
1900     {
1901       pc = parse_and_eval_address (address_arg);
1902       /* If we fail to find a section, that's ok, try the lookup anyway.  */
1903       section = find_pc_section (pc);
1904     }
1905 
1906   found = 0;
1907   for (objfile *objfile : current_program_space->objfiles ())
1908     {
1909       int printed_objfile_header = 0;
1910       int print_for_objfile = 1;
1911 
1912       QUIT;
1913       if (objfile_arg != NULL)
1914 	print_for_objfile
1915 	  = compare_filenames_for_search (objfile_name (objfile),
1916 					  objfile_arg);
1917       if (!print_for_objfile)
1918 	continue;
1919 
1920       if (address_arg != NULL)
1921 	{
1922 	  struct bound_minimal_symbol msymbol = { NULL, NULL };
1923 
1924 	  /* We don't assume each pc has a unique objfile (this is for
1925 	     debugging).  */
1926 	  struct partial_symtab *ps = find_pc_sect_psymtab (objfile, pc,
1927 							    section, msymbol);
1928 	  if (ps != NULL)
1929 	    {
1930 	      if (!printed_objfile_header)
1931 		{
1932 		  outfile->printf ("\nPartial symtabs for objfile %s\n",
1933 				  objfile_name (objfile));
1934 		  printed_objfile_header = 1;
1935 		}
1936 	      dump_psymtab (objfile, ps, outfile);
1937 	      dump_psymtab_addrmap (objfile, ps, outfile);
1938 	      found = 1;
1939 	    }
1940 	}
1941       else
1942 	{
1943 	  for (partial_symtab *ps : require_partial_symbols (objfile, true))
1944 	    {
1945 	      int print_for_source = 0;
1946 
1947 	      QUIT;
1948 	      if (source_arg != NULL)
1949 		{
1950 		  print_for_source
1951 		    = compare_filenames_for_search (ps->filename, source_arg);
1952 		  found = 1;
1953 		}
1954 	      if (source_arg == NULL
1955 		  || print_for_source)
1956 		{
1957 		  if (!printed_objfile_header)
1958 		    {
1959 		      outfile->printf ("\nPartial symtabs for objfile %s\n",
1960 				       objfile_name (objfile));
1961 		      printed_objfile_header = 1;
1962 		    }
1963 		  dump_psymtab (objfile, ps, outfile);
1964 		  dump_psymtab_addrmap (objfile, ps, outfile);
1965 		}
1966 	    }
1967 	}
1968 
1969       /* If we're printing all the objfile's symbols dump the full addrmap.  */
1970 
1971       if (address_arg == NULL
1972 	  && source_arg == NULL
1973 	  && objfile->partial_symtabs->psymtabs_addrmap != NULL)
1974 	{
1975 	  outfile->puts ("\n");
1976 	  dump_psymtab_addrmap (objfile, NULL, outfile);
1977 	}
1978     }
1979 
1980   if (!found)
1981     {
1982       if (address_arg != NULL)
1983 	error (_("No partial symtab for address: %s"), address_arg);
1984       if (source_arg != NULL)
1985 	error (_("No partial symtab for source file: %s"), source_arg);
1986     }
1987 }
1988 
1989 /* List all the partial symbol tables whose names match REGEXP (optional).  */
1990 
1991 static void
maintenance_info_psymtabs(const char * regexp,int from_tty)1992 maintenance_info_psymtabs (const char *regexp, int from_tty)
1993 {
1994   if (regexp)
1995     re_comp (regexp);
1996 
1997   for (struct program_space *pspace : program_spaces)
1998     for (objfile *objfile : pspace->objfiles ())
1999       {
2000 	struct gdbarch *gdbarch = objfile->arch ();
2001 
2002 	/* We don't want to print anything for this objfile until we
2003 	   actually find a symtab whose name matches.  */
2004 	int printed_objfile_start = 0;
2005 
2006 	for (partial_symtab *psymtab : require_partial_symbols (objfile, true))
2007 	  {
2008 	    QUIT;
2009 
2010 	    if (! regexp
2011 		|| re_exec (psymtab->filename))
2012 	      {
2013 		if (! printed_objfile_start)
2014 		  {
2015 		    printf_filtered ("{ objfile %s ", objfile_name (objfile));
2016 		    wrap_here ("  ");
2017 		    printf_filtered ("((struct objfile *) %s)\n",
2018 				     host_address_to_string (objfile));
2019 		    printed_objfile_start = 1;
2020 		  }
2021 
2022 		printf_filtered ("  { psymtab %s ", psymtab->filename);
2023 		wrap_here ("    ");
2024 		printf_filtered ("((struct partial_symtab *) %s)\n",
2025 				 host_address_to_string (psymtab));
2026 
2027 		printf_filtered ("    readin %s\n",
2028 				 psymtab->readin_p (objfile) ? "yes" : "no");
2029 		printf_filtered ("    fullname %s\n",
2030 				 psymtab->fullname
2031 				 ? psymtab->fullname : "(null)");
2032 		printf_filtered ("    text addresses ");
2033 		fputs_filtered (paddress (gdbarch,
2034 					  psymtab->text_low (objfile)),
2035 				gdb_stdout);
2036 		printf_filtered (" -- ");
2037 		fputs_filtered (paddress (gdbarch,
2038 					  psymtab->text_high (objfile)),
2039 				gdb_stdout);
2040 		printf_filtered ("\n");
2041 		printf_filtered ("    psymtabs_addrmap_supported %s\n",
2042 				 (psymtab->psymtabs_addrmap_supported
2043 				  ? "yes" : "no"));
2044 		printf_filtered ("    globals ");
2045 		if (psymtab->n_global_syms)
2046 		  {
2047 		    auto p = &(objfile->partial_symtabs
2048 			       ->global_psymbols[psymtab->globals_offset]);
2049 
2050 		    printf_filtered
2051 		      ("(* (struct partial_symbol **) %s @ %d)\n",
2052 		       host_address_to_string (p),
2053 		       psymtab->n_global_syms);
2054 		  }
2055 		else
2056 		  printf_filtered ("(none)\n");
2057 		printf_filtered ("    statics ");
2058 		if (psymtab->n_static_syms)
2059 		  {
2060 		    auto p = &(objfile->partial_symtabs
2061 			       ->static_psymbols[psymtab->statics_offset]);
2062 
2063 		    printf_filtered
2064 		      ("(* (struct partial_symbol **) %s @ %d)\n",
2065 		       host_address_to_string (p),
2066 		       psymtab->n_static_syms);
2067 		  }
2068 		else
2069 		  printf_filtered ("(none)\n");
2070 		if (psymtab->user)
2071 		  printf_filtered ("    user %s "
2072 				   "((struct partial_symtab *) %s)\n",
2073 				   psymtab->user->filename,
2074 				   host_address_to_string (psymtab->user));
2075 		printf_filtered ("    dependencies ");
2076 		if (psymtab->number_of_dependencies)
2077 		  {
2078 		    int i;
2079 
2080 		    printf_filtered ("{\n");
2081 		    for (i = 0; i < psymtab->number_of_dependencies; i++)
2082 		      {
2083 			struct partial_symtab *dep = psymtab->dependencies[i];
2084 
2085 			/* Note the string concatenation there --- no
2086 			   comma.  */
2087 			printf_filtered ("      psymtab %s "
2088 					 "((struct partial_symtab *) %s)\n",
2089 					 dep->filename,
2090 					 host_address_to_string (dep));
2091 		      }
2092 		    printf_filtered ("    }\n");
2093 		  }
2094 		else
2095 		  printf_filtered ("(none)\n");
2096 		printf_filtered ("  }\n");
2097 	      }
2098 	  }
2099 
2100 	if (printed_objfile_start)
2101 	  printf_filtered ("}\n");
2102       }
2103 }
2104 
2105 /* Check consistency of currently expanded psymtabs vs symtabs.  */
2106 
2107 static void
maintenance_check_psymtabs(const char * ignore,int from_tty)2108 maintenance_check_psymtabs (const char *ignore, int from_tty)
2109 {
2110   struct symbol *sym;
2111   struct compunit_symtab *cust = NULL;
2112   const struct blockvector *bv;
2113   const struct block *b;
2114   int i;
2115 
2116   for (objfile *objfile : current_program_space->objfiles ())
2117     for (partial_symtab *ps : require_partial_symbols (objfile, true))
2118       {
2119 	struct gdbarch *gdbarch = objfile->arch ();
2120 
2121 	/* We don't call psymtab_to_symtab here because that may cause symtab
2122 	   expansion.  When debugging a problem it helps if checkers leave
2123 	   things unchanged.  */
2124 	cust = ps->get_compunit_symtab (objfile);
2125 
2126 	/* First do some checks that don't require the associated symtab.  */
2127 	if (ps->text_high (objfile) < ps->text_low (objfile))
2128 	  {
2129 	    printf_filtered ("Psymtab ");
2130 	    puts_filtered (ps->filename);
2131 	    printf_filtered (" covers bad range ");
2132 	    fputs_filtered (paddress (gdbarch, ps->text_low (objfile)),
2133 			    gdb_stdout);
2134 	    printf_filtered (" - ");
2135 	    fputs_filtered (paddress (gdbarch, ps->text_high (objfile)),
2136 			    gdb_stdout);
2137 	    printf_filtered ("\n");
2138 	    continue;
2139 	  }
2140 
2141 	/* Now do checks requiring the associated symtab.  */
2142 	if (cust == NULL)
2143 	  continue;
2144 	bv = COMPUNIT_BLOCKVECTOR (cust);
2145 	b = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
2146 	partial_symbol **psym
2147 	  = &objfile->partial_symtabs->static_psymbols[ps->statics_offset];
2148 	for (i = 0; i < ps->n_static_syms; psym++, i++)
2149 	  {
2150 	    /* Skip symbols for inlined functions without address.  These may
2151 	       or may not have a match in the full symtab.  */
2152 	    if ((*psym)->aclass == LOC_BLOCK
2153 		&& (*psym)->ginfo.value.address == 0)
2154 	      continue;
2155 
2156 	    sym = block_lookup_symbol (b, (*psym)->ginfo.search_name (),
2157 				       symbol_name_match_type::SEARCH_NAME,
2158 				       (*psym)->domain);
2159 	    if (!sym)
2160 	      {
2161 		printf_filtered ("Static symbol `");
2162 		puts_filtered ((*psym)->ginfo.linkage_name ());
2163 		printf_filtered ("' only found in ");
2164 		puts_filtered (ps->filename);
2165 		printf_filtered (" psymtab\n");
2166 	      }
2167 	  }
2168 	b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2169 	psym = &objfile->partial_symtabs->global_psymbols[ps->globals_offset];
2170 	for (i = 0; i < ps->n_global_syms; psym++, i++)
2171 	  {
2172 	    sym = block_lookup_symbol (b, (*psym)->ginfo.search_name (),
2173 				       symbol_name_match_type::SEARCH_NAME,
2174 				       (*psym)->domain);
2175 	    if (!sym)
2176 	      {
2177 		printf_filtered ("Global symbol `");
2178 		puts_filtered ((*psym)->ginfo.linkage_name ());
2179 		printf_filtered ("' only found in ");
2180 		puts_filtered (ps->filename);
2181 		printf_filtered (" psymtab\n");
2182 	      }
2183 	  }
2184 	if (ps->raw_text_high () != 0
2185 	    && (ps->text_low (objfile) < BLOCK_START (b)
2186 		|| ps->text_high (objfile) > BLOCK_END (b)))
2187 	  {
2188 	    printf_filtered ("Psymtab ");
2189 	    puts_filtered (ps->filename);
2190 	    printf_filtered (" covers ");
2191 	    fputs_filtered (paddress (gdbarch, ps->text_low (objfile)),
2192 			    gdb_stdout);
2193 	    printf_filtered (" - ");
2194 	    fputs_filtered (paddress (gdbarch, ps->text_high (objfile)),
2195 			    gdb_stdout);
2196 	    printf_filtered (" but symtab covers only ");
2197 	    fputs_filtered (paddress (gdbarch, BLOCK_START (b)), gdb_stdout);
2198 	    printf_filtered (" - ");
2199 	    fputs_filtered (paddress (gdbarch, BLOCK_END (b)), gdb_stdout);
2200 	    printf_filtered ("\n");
2201 	  }
2202       }
2203 }
2204 
2205 void _initialize_psymtab ();
2206 void
_initialize_psymtab()2207 _initialize_psymtab ()
2208 {
2209   add_cmd ("psymbols", class_maintenance, maintenance_print_psymbols, _("\
2210 Print dump of current partial symbol definitions.\n\
2211 Usage: mt print psymbols [-objfile OBJFILE] [-pc ADDRESS] [--] [OUTFILE]\n\
2212        mt print psymbols [-objfile OBJFILE] [-source SOURCE] [--] [OUTFILE]\n\
2213 Entries in the partial symbol table are dumped to file OUTFILE,\n\
2214 or the terminal if OUTFILE is unspecified.\n\
2215 If ADDRESS is provided, dump only the file for that address.\n\
2216 If SOURCE is provided, dump only that file's symbols.\n\
2217 If OBJFILE is provided, dump only that file's minimal symbols."),
2218 	   &maintenanceprintlist);
2219 
2220   add_cmd ("psymtabs", class_maintenance, maintenance_info_psymtabs, _("\
2221 List the partial symbol tables for all object files.\n\
2222 This does not include information about individual partial symbols,\n\
2223 just the symbol table structures themselves."),
2224 	   &maintenanceinfolist);
2225 
2226   add_cmd ("check-psymtabs", class_maintenance, maintenance_check_psymtabs,
2227 	   _("\
2228 Check consistency of currently expanded psymtabs versus symtabs."),
2229 	   &maintenancelist);
2230 }
2231