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