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