1 /* Partial symbol tables.
3 Copyright (C) 2009-2018 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
25 #include "filenames.h"
32 #include "readline/readline.h"
33 #include "gdb_regex.h"
34 #include "dictionary.h"
36 #include "cp-support.h"
43 struct bcache
*bcache
;
46 static struct partial_symbol
*match_partial_symbol (struct objfile
*,
47 struct partial_symtab
*,
49 const char *, domain_enum
,
50 symbol_name_match_type
,
51 symbol_compare_ftype
*);
53 static struct partial_symbol
*lookup_partial_symbol (struct objfile
*,
54 struct partial_symtab
*,
58 static const char *psymtab_to_fullname (struct partial_symtab
*ps
);
60 static struct partial_symbol
*find_pc_sect_psymbol (struct objfile
*,
61 struct partial_symtab
*,
63 struct obj_section
*);
65 static void fixup_psymbol_section (struct partial_symbol
*psym
,
66 struct objfile
*objfile
);
68 static struct compunit_symtab
*psymtab_to_symtab (struct objfile
*objfile
,
69 struct partial_symtab
*pst
);
71 /* Ensure that the partial symbols for OBJFILE have been loaded. This
72 function always returns its argument, as a convenience. */
75 require_partial_symbols (struct objfile
*objfile
, int verbose
)
77 if ((objfile
->flags
& OBJF_PSYMTABS_READ
) == 0)
79 objfile
->flags
|= OBJF_PSYMTABS_READ
;
81 if (objfile
->sf
->sym_read_psymbols
)
85 printf_unfiltered (_("Reading symbols from %s..."),
86 objfile_name (objfile
));
87 gdb_flush (gdb_stdout
);
89 (*objfile
->sf
->sym_read_psymbols
) (objfile
);
91 /* Partial symbols list are not expected to changed after this
93 objfile
->global_psymbols
.shrink_to_fit ();
94 objfile
->static_psymbols
.shrink_to_fit ();
98 if (!objfile_has_symbols (objfile
))
101 printf_unfiltered (_("(no debugging symbols found)..."));
105 printf_unfiltered (_("done.\n"));
113 /* Traverse all psymtabs in one objfile, requiring that the psymtabs
116 #define ALL_OBJFILE_PSYMTABS_REQUIRED(objfile, p) \
117 for ((p) = require_partial_symbols (objfile, 1)->psymtabs; \
121 /* We want to make sure this file always requires psymtabs. */
123 #undef ALL_OBJFILE_PSYMTABS
125 /* Traverse all psymtabs in all objfiles. */
127 #define ALL_PSYMTABS(objfile, p) \
128 ALL_OBJFILES (objfile) \
129 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, p)
131 /* Helper function for psym_map_symtabs_matching_filename that
132 expands the symtabs and calls the iterator. */
135 partial_map_expand_apply (struct objfile
*objfile
,
137 const char *real_path
,
138 struct partial_symtab
*pst
,
139 gdb::function_view
<bool (symtab
*)> callback
)
141 struct compunit_symtab
*last_made
= objfile
->compunit_symtabs
;
143 /* Shared psymtabs should never be seen here. Instead they should
144 be handled properly by the caller. */
145 gdb_assert (pst
->user
== NULL
);
147 /* Don't visit already-expanded psymtabs. */
151 /* This may expand more than one symtab, and we want to iterate over
153 psymtab_to_symtab (objfile
, pst
);
155 return iterate_over_some_symtabs (name
, real_path
, objfile
->compunit_symtabs
,
156 last_made
, callback
);
159 /* Psymtab version of map_symtabs_matching_filename. See its definition in
160 the definition of quick_symbol_functions in symfile.h. */
163 psym_map_symtabs_matching_filename
164 (struct objfile
*objfile
,
166 const char *real_path
,
167 gdb::function_view
<bool (symtab
*)> callback
)
169 struct partial_symtab
*pst
;
170 const char *name_basename
= lbasename (name
);
172 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile
, pst
)
174 /* We can skip shared psymtabs here, because any file name will be
175 attached to the unshared psymtab. */
176 if (pst
->user
!= NULL
)
179 /* Anonymous psymtabs don't have a file name. */
183 if (compare_filenames_for_search (pst
->filename
, name
))
185 if (partial_map_expand_apply (objfile
, name
, real_path
,
191 /* Before we invoke realpath, which can get expensive when many
192 files are involved, do a quick comparison of the basenames. */
193 if (! basenames_may_differ
194 && FILENAME_CMP (name_basename
, lbasename (pst
->filename
)) != 0)
197 if (compare_filenames_for_search (psymtab_to_fullname (pst
), name
))
199 if (partial_map_expand_apply (objfile
, name
, real_path
,
205 /* If the user gave us an absolute path, try to find the file in
206 this symtab and use its absolute path. */
207 if (real_path
!= NULL
)
209 gdb_assert (IS_ABSOLUTE_PATH (real_path
));
210 gdb_assert (IS_ABSOLUTE_PATH (name
));
211 if (filename_cmp (psymtab_to_fullname (pst
), real_path
) == 0)
213 if (partial_map_expand_apply (objfile
, name
, real_path
,
224 /* Find which partial symtab contains PC and SECTION starting at psymtab PST.
225 We may find a different psymtab than PST. See FIND_PC_SECT_PSYMTAB. */
227 static struct partial_symtab
*
228 find_pc_sect_psymtab_closer (struct objfile
*objfile
,
229 CORE_ADDR pc
, struct obj_section
*section
,
230 struct partial_symtab
*pst
,
231 struct bound_minimal_symbol msymbol
)
233 struct partial_symtab
*tpst
;
234 struct partial_symtab
*best_pst
= pst
;
235 CORE_ADDR best_addr
= pst
->textlow
;
237 gdb_assert (!pst
->psymtabs_addrmap_supported
);
239 /* An objfile that has its functions reordered might have
240 many partial symbol tables containing the PC, but
241 we want the partial symbol table that contains the
242 function containing the PC. */
243 if (!(objfile
->flags
& OBJF_REORDERED
)
244 && section
== NULL
) /* Can't validate section this way. */
247 if (msymbol
.minsym
== NULL
)
250 /* The code range of partial symtabs sometimes overlap, so, in
251 the loop below, we need to check all partial symtabs and
252 find the one that fits better for the given PC address. We
253 select the partial symtab that contains a symbol whose
254 address is closest to the PC address. By closest we mean
255 that find_pc_sect_symbol returns the symbol with address
256 that is closest and still less than the given PC. */
257 for (tpst
= pst
; tpst
!= NULL
; tpst
= tpst
->next
)
259 if (pc
>= tpst
->textlow
&& pc
< tpst
->texthigh
)
261 struct partial_symbol
*p
;
264 /* NOTE: This assumes that every psymbol has a
265 corresponding msymbol, which is not necessarily
266 true; the debug info might be much richer than the
267 object's symbol table. */
268 p
= find_pc_sect_psymbol (objfile
, tpst
, pc
, section
);
270 && (p
->value
.address
== BMSYMBOL_VALUE_ADDRESS (msymbol
)))
273 /* Also accept the textlow value of a psymtab as a
274 "symbol", to provide some support for partial
275 symbol tables with line information but no debug
276 symbols (e.g. those produced by an assembler). */
278 this_addr
= p
->value
.address
;
280 this_addr
= tpst
->textlow
;
282 /* Check whether it is closer than our current
283 BEST_ADDR. Since this symbol address is
284 necessarily lower or equal to PC, the symbol closer
285 to PC is the symbol which address is the highest.
286 This way we return the psymtab which contains such
287 best match symbol. This can help in cases where the
288 symbol information/debuginfo is not complete, like
289 for instance on IRIX6 with gcc, where no debug info
290 is emitted for statics. (See also the nodebug.exp
292 if (this_addr
> best_addr
)
294 best_addr
= this_addr
;
302 /* Find which partial symtab contains PC and SECTION. Return NULL if
303 none. We return the psymtab that contains a symbol whose address
304 exactly matches PC, or, if we cannot find an exact match, the
305 psymtab that contains a symbol whose address is closest to PC. */
307 static struct partial_symtab
*
308 find_pc_sect_psymtab (struct objfile
*objfile
, CORE_ADDR pc
,
309 struct obj_section
*section
,
310 struct bound_minimal_symbol msymbol
)
312 struct partial_symtab
*pst
;
314 /* Try just the PSYMTABS_ADDRMAP mapping first as it has better granularity
315 than the later used TEXTLOW/TEXTHIGH one. */
317 if (objfile
->psymtabs_addrmap
!= NULL
)
319 pst
= ((struct partial_symtab
*)
320 addrmap_find (objfile
->psymtabs_addrmap
, pc
));
323 /* FIXME: addrmaps currently do not handle overlayed sections,
324 so fall back to the non-addrmap case if we're debugging
325 overlays and the addrmap returned the wrong section. */
326 if (overlay_debugging
&& msymbol
.minsym
!= NULL
&& section
!= NULL
)
328 struct partial_symbol
*p
;
330 /* NOTE: This assumes that every psymbol has a
331 corresponding msymbol, which is not necessarily
332 true; the debug info might be much richer than the
333 object's symbol table. */
334 p
= find_pc_sect_psymbol (objfile
, pst
, pc
, section
);
336 || (p
->value
.address
!= BMSYMBOL_VALUE_ADDRESS (msymbol
)))
340 /* We do not try to call FIND_PC_SECT_PSYMTAB_CLOSER as
341 PSYMTABS_ADDRMAP we used has already the best 1-byte
342 granularity and FIND_PC_SECT_PSYMTAB_CLOSER may mislead us into
343 a worse chosen section due to the TEXTLOW/TEXTHIGH ranges
352 /* Existing PSYMTABS_ADDRMAP mapping is present even for PARTIAL_SYMTABs
353 which still have no corresponding full SYMTABs read. But it is not
354 present for non-DWARF2 debug infos not supporting PSYMTABS_ADDRMAP in GDB
357 /* Check even OBJFILE with non-zero PSYMTABS_ADDRMAP as only several of
358 its CUs may be missing in PSYMTABS_ADDRMAP as they may be varying
359 debug info type in single OBJFILE. */
361 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile
, pst
)
362 if (!pst
->psymtabs_addrmap_supported
363 && pc
>= pst
->textlow
&& pc
< pst
->texthigh
)
365 struct partial_symtab
*best_pst
;
367 best_pst
= find_pc_sect_psymtab_closer (objfile
, pc
, section
, pst
,
369 if (best_pst
!= NULL
)
376 /* Psymtab version of find_pc_sect_compunit_symtab. See its definition in
377 the definition of quick_symbol_functions in symfile.h. */
379 static struct compunit_symtab
*
380 psym_find_pc_sect_compunit_symtab (struct objfile
*objfile
,
381 struct bound_minimal_symbol msymbol
,
383 struct obj_section
*section
,
386 struct partial_symtab
*ps
= find_pc_sect_psymtab (objfile
, pc
, section
,
390 if (warn_if_readin
&& ps
->readin
)
391 /* Might want to error() here (in case symtab is corrupt and
392 will cause a core dump), but maybe we can successfully
393 continue, so let's not. */
395 (Internal error: pc %s in read in psymtab, but not in symtab.)\n"),
396 paddress (get_objfile_arch (objfile
), pc
));
397 psymtab_to_symtab (objfile
, ps
);
398 return ps
->compunit_symtab
;
403 /* Find which partial symbol within a psymtab matches PC and SECTION.
404 Return NULL if none. */
406 static struct partial_symbol
*
407 find_pc_sect_psymbol (struct objfile
*objfile
,
408 struct partial_symtab
*psymtab
, CORE_ADDR pc
,
409 struct obj_section
*section
)
411 struct partial_symbol
*best
= NULL
;
414 gdb_assert (psymtab
!= NULL
);
416 /* Cope with programs that start at address 0. */
417 best_pc
= (psymtab
->textlow
!= 0) ? psymtab
->textlow
- 1 : 0;
419 /* Search the global symbols as well as the static symbols, so that
420 find_pc_partial_function doesn't use a minimal symbol and thus
421 cache a bad endaddr. */
422 for (int i
= 0; i
< psymtab
->n_global_syms
; i
++)
424 partial_symbol
*p
= objfile
->global_psymbols
[psymtab
->globals_offset
+ i
];
426 if (p
->domain
== VAR_DOMAIN
427 && p
->aclass
== LOC_BLOCK
428 && pc
>= p
->value
.address
429 && (p
->value
.address
> best_pc
430 || (psymtab
->textlow
== 0
431 && best_pc
== 0 && p
->value
.address
== 0)))
433 if (section
!= NULL
) /* Match on a specific section. */
435 fixup_psymbol_section (p
, objfile
);
436 if (!matching_obj_sections (p
->obj_section (objfile
),
440 best_pc
= p
->value
.address
;
445 for (int i
= 0; i
< psymtab
->n_static_syms
; i
++)
447 partial_symbol
*p
= objfile
->static_psymbols
[psymtab
->statics_offset
+ i
];
449 if (p
->domain
== VAR_DOMAIN
450 && p
->aclass
== LOC_BLOCK
451 && pc
>= p
->value
.address
452 && (p
->value
.address
> best_pc
453 || (psymtab
->textlow
== 0
454 && best_pc
== 0 && p
->value
.address
== 0)))
456 if (section
!= NULL
) /* Match on a specific section. */
458 fixup_psymbol_section (p
, objfile
);
459 if (!matching_obj_sections (p
->obj_section (objfile
),
463 best_pc
= p
->value
.address
;
472 fixup_psymbol_section (struct partial_symbol
*psym
, struct objfile
*objfile
)
479 if (psym
->section
>= 0)
482 gdb_assert (objfile
);
484 switch (psym
->aclass
)
489 addr
= psym
->value
.address
;
492 /* Nothing else will be listed in the minsyms -- no use looking
497 fixup_section (psym
, addr
, objfile
);
500 /* Psymtab version of lookup_symbol. See its definition in
501 the definition of quick_symbol_functions in symfile.h. */
503 static struct compunit_symtab
*
504 psym_lookup_symbol (struct objfile
*objfile
,
505 int block_index
, const char *name
,
506 const domain_enum domain
)
508 struct partial_symtab
*ps
;
509 const int psymtab_index
= (block_index
== GLOBAL_BLOCK
? 1 : 0);
510 struct compunit_symtab
*stab_best
= NULL
;
512 lookup_name_info
lookup_name (name
, symbol_name_match_type::FULL
);
514 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile
, ps
)
516 if (!ps
->readin
&& lookup_partial_symbol (objfile
, ps
, name
,
517 psymtab_index
, domain
))
519 struct symbol
*sym
, *with_opaque
= NULL
;
520 struct compunit_symtab
*stab
= psymtab_to_symtab (objfile
, ps
);
521 /* Note: While psymtab_to_symtab can return NULL if the partial symtab
522 is empty, we can assume it won't here because lookup_partial_symbol
524 const struct blockvector
*bv
= COMPUNIT_BLOCKVECTOR (stab
);
525 struct block
*block
= BLOCKVECTOR_BLOCK (bv
, block_index
);
527 sym
= block_find_symbol (block
, name
, domain
,
528 block_find_non_opaque_type_preferred
,
531 /* Some caution must be observed with overloaded functions
532 and methods, since the index will not contain any overload
533 information (but NAME might contain it). */
536 && SYMBOL_MATCHES_SEARCH_NAME (sym
, lookup_name
))
538 if (with_opaque
!= NULL
539 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque
, lookup_name
))
542 /* Keep looking through other psymtabs. */
549 /* Returns true if PSYM matches LOOKUP_NAME. */
552 psymbol_name_matches (partial_symbol
*psym
,
553 const lookup_name_info
&lookup_name
)
555 const language_defn
*lang
= language_def (psym
->language
);
556 symbol_name_matcher_ftype
*name_match
557 = get_symbol_name_matcher (lang
, lookup_name
);
558 return name_match (symbol_search_name (psym
), lookup_name
, NULL
);
561 /* Look in PST for a symbol in DOMAIN whose name matches NAME. Search
562 the global block of PST if GLOBAL, and otherwise the static block.
563 MATCH is the comparison operation that returns true iff MATCH (s,
564 NAME), where s is a SYMBOL_SEARCH_NAME. If ORDERED_COMPARE is
565 non-null, the symbols in the block are assumed to be ordered
566 according to it (allowing binary search). It must be compatible
567 with MATCH. Returns the symbol, if found, and otherwise NULL. */
569 static struct partial_symbol
*
570 match_partial_symbol (struct objfile
*objfile
,
571 struct partial_symtab
*pst
, int global
,
572 const char *name
, domain_enum domain
,
573 symbol_name_match_type match_type
,
574 symbol_compare_ftype
*ordered_compare
)
576 struct partial_symbol
**start
, **psym
;
577 struct partial_symbol
**top
, **real_top
, **bottom
, **center
;
578 int length
= (global
? pst
->n_global_syms
: pst
->n_static_syms
);
579 int do_linear_search
= 1;
584 lookup_name_info
lookup_name (name
, match_type
);
587 &objfile
->global_psymbols
[pst
->globals_offset
] :
588 &objfile
->static_psymbols
[pst
->statics_offset
]);
590 if (global
&& ordered_compare
) /* Can use a binary search. */
592 do_linear_search
= 0;
594 /* Binary search. This search is guaranteed to end with center
595 pointing at the earliest partial symbol whose name might be
596 correct. At that point *all* partial symbols with an
597 appropriate name will be checked against the correct
601 top
= start
+ length
- 1;
605 center
= bottom
+ (top
- bottom
) / 2;
606 gdb_assert (center
< top
);
608 enum language lang
= (*center
)->language
;
610 = lookup_name
.language_lookup_name (lang
).c_str ();
612 if (ordered_compare (symbol_search_name (*center
), lang_ln
) >= 0)
617 gdb_assert (top
== bottom
);
619 while (top
<= real_top
620 && psymbol_name_matches (*top
, lookup_name
))
622 if (symbol_matches_domain ((*top
)->language
,
623 (*top
)->domain
, domain
))
629 /* Can't use a binary search or else we found during the binary search that
630 we should also do a linear search. */
632 if (do_linear_search
)
634 for (psym
= start
; psym
< start
+ length
; psym
++)
636 if (symbol_matches_domain ((*psym
)->language
,
637 (*psym
)->domain
, domain
)
638 && psymbol_name_matches (*psym
, lookup_name
))
646 /* Returns the name used to search psymtabs. Unlike symtabs, psymtabs do
647 not contain any method/function instance information (since this would
648 force reading type information while reading psymtabs). Therefore,
649 if NAME contains overload information, it must be stripped before searching
652 static gdb::unique_xmalloc_ptr
<char>
653 psymtab_search_name (const char *name
)
655 switch (current_language
->la_language
)
659 if (strchr (name
, '('))
661 gdb::unique_xmalloc_ptr
<char> ret
= cp_remove_params (name
);
673 return gdb::unique_xmalloc_ptr
<char> (xstrdup (name
));
676 /* Look, in partial_symtab PST, for symbol whose natural name is NAME.
677 Check the global symbols if GLOBAL, the static symbols if not. */
679 static struct partial_symbol
*
680 lookup_partial_symbol (struct objfile
*objfile
,
681 struct partial_symtab
*pst
, const char *name
,
682 int global
, domain_enum domain
)
684 struct partial_symbol
**start
, **psym
;
685 struct partial_symbol
**top
, **real_top
, **bottom
, **center
;
686 int length
= (global
? pst
->n_global_syms
: pst
->n_static_syms
);
687 int do_linear_search
= 1;
692 gdb::unique_xmalloc_ptr
<char> search_name
= psymtab_search_name (name
);
694 lookup_name_info
lookup_name (search_name
.get (), symbol_name_match_type::FULL
);
697 &objfile
->global_psymbols
[pst
->globals_offset
] :
698 &objfile
->static_psymbols
[pst
->statics_offset
]);
700 if (global
) /* This means we can use a binary search. */
702 do_linear_search
= 0;
704 /* Binary search. This search is guaranteed to end with center
705 pointing at the earliest partial symbol whose name might be
706 correct. At that point *all* partial symbols with an
707 appropriate name will be checked against the correct
711 top
= start
+ length
- 1;
715 center
= bottom
+ (top
- bottom
) / 2;
717 internal_error (__FILE__
, __LINE__
,
718 _("failed internal consistency check"));
719 if (strcmp_iw_ordered (symbol_search_name (*center
),
720 search_name
.get ()) >= 0)
729 if (!(top
== bottom
))
730 internal_error (__FILE__
, __LINE__
,
731 _("failed internal consistency check"));
733 /* For `case_sensitivity == case_sensitive_off' strcmp_iw_ordered will
734 search more exactly than what matches SYMBOL_MATCHES_SEARCH_NAME. */
735 while (top
>= start
&& symbol_matches_search_name (*top
, lookup_name
))
738 /* Fixup to have a symbol which matches SYMBOL_MATCHES_SEARCH_NAME. */
741 while (top
<= real_top
&& symbol_matches_search_name (*top
, lookup_name
))
743 if (symbol_matches_domain ((*top
)->language
,
744 (*top
)->domain
, domain
))
750 /* Can't use a binary search or else we found during the binary search that
751 we should also do a linear search. */
753 if (do_linear_search
)
755 for (psym
= start
; psym
< start
+ length
; psym
++)
757 if (symbol_matches_domain ((*psym
)->language
,
758 (*psym
)->domain
, domain
)
759 && symbol_matches_search_name (*psym
, lookup_name
))
767 /* Get the symbol table that corresponds to a partial_symtab.
768 This is fast after the first time you do it.
769 The result will be NULL if the primary symtab has no symbols,
770 which can happen. Otherwise the result is the primary symtab
771 that contains PST. */
773 static struct compunit_symtab
*
774 psymtab_to_symtab (struct objfile
*objfile
, struct partial_symtab
*pst
)
776 /* If it is a shared psymtab, find an unshared psymtab that includes
777 it. Any such psymtab will do. */
778 while (pst
->user
!= NULL
)
781 /* If it's been looked up before, return it. */
782 if (pst
->compunit_symtab
)
783 return pst
->compunit_symtab
;
785 /* If it has not yet been read in, read it. */
788 scoped_restore decrementer
= increment_reading_symtab ();
790 (*pst
->read_symtab
) (pst
, objfile
);
793 return pst
->compunit_symtab
;
796 /* Psymtab version of relocate. See its definition in
797 the definition of quick_symbol_functions in symfile.h. */
800 psym_relocate (struct objfile
*objfile
,
801 const struct section_offsets
*new_offsets
,
802 const struct section_offsets
*delta
)
804 struct partial_symtab
*p
;
806 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile
, p
)
808 p
->textlow
+= ANOFFSET (delta
, SECT_OFF_TEXT (objfile
));
809 p
->texthigh
+= ANOFFSET (delta
, SECT_OFF_TEXT (objfile
));
812 for (partial_symbol
*psym
: objfile
->global_psymbols
)
814 fixup_psymbol_section (psym
, objfile
);
815 if (psym
->section
>= 0)
816 psym
->value
.address
+= ANOFFSET (delta
, psym
->section
);
818 for (partial_symbol
*psym
: objfile
->static_psymbols
)
820 fixup_psymbol_section (psym
, objfile
);
821 if (psym
->section
>= 0)
822 psym
->value
.address
+= ANOFFSET (delta
, psym
->section
);
825 objfile
->psymbol_map
.clear ();
828 /* Psymtab version of find_last_source_symtab. See its definition in
829 the definition of quick_symbol_functions in symfile.h. */
831 static struct symtab
*
832 psym_find_last_source_symtab (struct objfile
*ofp
)
834 struct partial_symtab
*ps
;
835 struct partial_symtab
*cs_pst
= NULL
;
837 ALL_OBJFILE_PSYMTABS_REQUIRED (ofp
, ps
)
839 const char *name
= ps
->filename
;
840 int len
= strlen (name
);
842 if (!(len
> 2 && (strcmp (&name
[len
- 2], ".h") == 0
843 || strcmp (name
, "<<C++-namespaces>>") == 0)))
851 internal_error (__FILE__
, __LINE__
,
852 _("select_source_symtab: "
853 "readin pst found and no symtabs."));
857 struct compunit_symtab
*cust
= psymtab_to_symtab (ofp
, cs_pst
);
861 return compunit_primary_filetab (cust
);
867 /* Psymtab version of forget_cached_source_info. See its definition in
868 the definition of quick_symbol_functions in symfile.h. */
871 psym_forget_cached_source_info (struct objfile
*objfile
)
873 struct partial_symtab
*pst
;
875 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile
, pst
)
877 if (pst
->fullname
!= NULL
)
879 xfree (pst
->fullname
);
880 pst
->fullname
= NULL
;
886 print_partial_symbols (struct gdbarch
*gdbarch
,
887 struct partial_symbol
**p
, int count
, const char *what
,
888 struct ui_file
*outfile
)
890 fprintf_filtered (outfile
, " %s partial symbols:\n", what
);
894 fprintf_filtered (outfile
, " `%s'", (*p
)->name
);
895 if (symbol_demangled_name (*p
) != NULL
)
897 fprintf_filtered (outfile
, " `%s'", symbol_demangled_name (*p
));
899 fputs_filtered (", ", outfile
);
900 switch ((*p
)->domain
)
903 fputs_filtered ("undefined domain, ", outfile
);
906 /* This is the usual thing -- don't print it. */
909 fputs_filtered ("struct domain, ", outfile
);
912 fputs_filtered ("label domain, ", outfile
);
915 fputs_filtered ("<invalid domain>, ", outfile
);
918 switch ((*p
)->aclass
)
921 fputs_filtered ("undefined", outfile
);
924 fputs_filtered ("constant int", outfile
);
927 fputs_filtered ("static", outfile
);
930 fputs_filtered ("register", outfile
);
933 fputs_filtered ("pass by value", outfile
);
936 fputs_filtered ("pass by reference", outfile
);
938 case LOC_REGPARM_ADDR
:
939 fputs_filtered ("register address parameter", outfile
);
942 fputs_filtered ("stack parameter", outfile
);
945 fputs_filtered ("type", outfile
);
948 fputs_filtered ("label", outfile
);
951 fputs_filtered ("function", outfile
);
953 case LOC_CONST_BYTES
:
954 fputs_filtered ("constant bytes", outfile
);
957 fputs_filtered ("unresolved", outfile
);
959 case LOC_OPTIMIZED_OUT
:
960 fputs_filtered ("optimized out", outfile
);
963 fputs_filtered ("computed at runtime", outfile
);
966 fputs_filtered ("<invalid location>", outfile
);
969 fputs_filtered (", ", outfile
);
970 fputs_filtered (paddress (gdbarch
, (*p
)->value
.address
), outfile
);
971 fprintf_filtered (outfile
, "\n");
977 dump_psymtab (struct objfile
*objfile
, struct partial_symtab
*psymtab
,
978 struct ui_file
*outfile
)
980 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
983 if (psymtab
->anonymous
)
985 fprintf_filtered (outfile
, "\nAnonymous partial symtab (%s) ",
990 fprintf_filtered (outfile
, "\nPartial symtab for source file %s ",
993 fprintf_filtered (outfile
, "(object ");
994 gdb_print_host_address (psymtab
, outfile
);
995 fprintf_filtered (outfile
, ")\n\n");
996 fprintf_unfiltered (outfile
, " Read from object file %s (",
997 objfile_name (objfile
));
998 gdb_print_host_address (objfile
, outfile
);
999 fprintf_unfiltered (outfile
, ")\n");
1001 if (psymtab
->readin
)
1003 fprintf_filtered (outfile
,
1004 " Full symtab was read (at ");
1005 gdb_print_host_address (psymtab
->compunit_symtab
, outfile
);
1006 fprintf_filtered (outfile
, " by function at ");
1007 gdb_print_host_address (psymtab
->read_symtab
, outfile
);
1008 fprintf_filtered (outfile
, ")\n");
1011 fprintf_filtered (outfile
, " Symbols cover text addresses ");
1012 fputs_filtered (paddress (gdbarch
, psymtab
->textlow
), outfile
);
1013 fprintf_filtered (outfile
, "-");
1014 fputs_filtered (paddress (gdbarch
, psymtab
->texthigh
), outfile
);
1015 fprintf_filtered (outfile
, "\n");
1016 fprintf_filtered (outfile
, " Address map supported - %s.\n",
1017 psymtab
->psymtabs_addrmap_supported
? "yes" : "no");
1018 fprintf_filtered (outfile
, " Depends on %d other partial symtabs.\n",
1019 psymtab
->number_of_dependencies
);
1020 for (i
= 0; i
< psymtab
->number_of_dependencies
; i
++)
1022 fprintf_filtered (outfile
, " %d ", i
);
1023 gdb_print_host_address (psymtab
->dependencies
[i
], outfile
);
1024 fprintf_filtered (outfile
, " %s\n",
1025 psymtab
->dependencies
[i
]->filename
);
1027 if (psymtab
->user
!= NULL
)
1029 fprintf_filtered (outfile
, " Shared partial symtab with user ");
1030 gdb_print_host_address (psymtab
->user
, outfile
);
1031 fprintf_filtered (outfile
, "\n");
1033 if (psymtab
->n_global_syms
> 0)
1035 print_partial_symbols (gdbarch
,
1036 &objfile
->global_psymbols
[psymtab
->globals_offset
],
1037 psymtab
->n_global_syms
, "Global", outfile
);
1039 if (psymtab
->n_static_syms
> 0)
1041 print_partial_symbols (gdbarch
,
1042 &objfile
->static_psymbols
[psymtab
->statics_offset
],
1043 psymtab
->n_static_syms
, "Static", outfile
);
1045 fprintf_filtered (outfile
, "\n");
1048 /* Psymtab version of print_stats. See its definition in
1049 the definition of quick_symbol_functions in symfile.h. */
1052 psym_print_stats (struct objfile
*objfile
)
1055 struct partial_symtab
*ps
;
1058 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile
, ps
)
1060 if (ps
->readin
== 0)
1063 printf_filtered (_(" Number of psym tables (not yet expanded): %d\n"), i
);
1066 /* Psymtab version of dump. See its definition in
1067 the definition of quick_symbol_functions in symfile.h. */
1070 psym_dump (struct objfile
*objfile
)
1072 struct partial_symtab
*psymtab
;
1074 if (objfile
->psymtabs
)
1076 printf_filtered ("Psymtabs:\n");
1077 for (psymtab
= objfile
->psymtabs
;
1079 psymtab
= psymtab
->next
)
1081 printf_filtered ("%s at ",
1083 gdb_print_host_address (psymtab
, gdb_stdout
);
1084 printf_filtered (", ");
1087 printf_filtered ("\n\n");
1091 /* Psymtab version of expand_symtabs_for_function. See its definition in
1092 the definition of quick_symbol_functions in symfile.h. */
1095 psym_expand_symtabs_for_function (struct objfile
*objfile
,
1096 const char *func_name
)
1098 struct partial_symtab
*ps
;
1100 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile
, ps
)
1105 if ((lookup_partial_symbol (objfile
, ps
, func_name
, 1, VAR_DOMAIN
)
1107 || (lookup_partial_symbol (objfile
, ps
, func_name
, 0, VAR_DOMAIN
)
1109 psymtab_to_symtab (objfile
, ps
);
1113 /* Psymtab version of expand_all_symtabs. See its definition in
1114 the definition of quick_symbol_functions in symfile.h. */
1117 psym_expand_all_symtabs (struct objfile
*objfile
)
1119 struct partial_symtab
*psymtab
;
1121 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile
, psymtab
)
1123 psymtab_to_symtab (objfile
, psymtab
);
1127 /* Psymtab version of expand_symtabs_with_fullname. See its definition in
1128 the definition of quick_symbol_functions in symfile.h. */
1131 psym_expand_symtabs_with_fullname (struct objfile
*objfile
,
1132 const char *fullname
)
1134 struct partial_symtab
*p
;
1136 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile
, p
)
1138 /* Anonymous psymtabs don't have a name of a source file. */
1142 /* psymtab_to_fullname tries to open the file which is slow.
1143 Don't call it if we know the basenames don't match. */
1144 if ((basenames_may_differ
1145 || filename_cmp (lbasename (fullname
), lbasename (p
->filename
)) == 0)
1146 && filename_cmp (fullname
, psymtab_to_fullname (p
)) == 0)
1147 psymtab_to_symtab (objfile
, p
);
1151 /* Psymtab version of map_symbol_filenames. See its definition in
1152 the definition of quick_symbol_functions in symfile.h. */
1155 psym_map_symbol_filenames (struct objfile
*objfile
,
1156 symbol_filename_ftype
*fun
, void *data
,
1159 struct partial_symtab
*ps
;
1161 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile
, ps
)
1163 const char *fullname
;
1168 /* We can skip shared psymtabs here, because any file name will be
1169 attached to the unshared psymtab. */
1170 if (ps
->user
!= NULL
)
1173 /* Anonymous psymtabs don't have a file name. */
1179 fullname
= psymtab_to_fullname (ps
);
1182 (*fun
) (ps
->filename
, fullname
, data
);
1186 /* Finds the fullname that a partial_symtab represents.
1188 If this functions finds the fullname, it will save it in ps->fullname
1189 and it will also return the value.
1191 If this function fails to find the file that this partial_symtab represents,
1192 NULL will be returned and ps->fullname will be set to NULL. */
1195 psymtab_to_fullname (struct partial_symtab
*ps
)
1197 gdb_assert (!ps
->anonymous
);
1199 /* Use cached copy if we have it.
1200 We rely on forget_cached_source_info being called appropriately
1201 to handle cases like the file being moved. */
1202 if (ps
->fullname
== NULL
)
1204 gdb::unique_xmalloc_ptr
<char> fullname
;
1205 int fd
= find_and_open_source (ps
->filename
, ps
->dirname
, &fullname
);
1206 ps
->fullname
= fullname
.release ();
1212 /* rewrite_source_path would be applied by find_and_open_source, we
1213 should report the pathname where GDB tried to find the file. */
1215 if (ps
->dirname
== NULL
|| IS_ABSOLUTE_PATH (ps
->filename
))
1216 fullname
.reset (xstrdup (ps
->filename
));
1218 fullname
.reset (concat (ps
->dirname
, SLASH_STRING
,
1219 ps
->filename
, (char *) NULL
));
1221 ps
->fullname
= rewrite_source_path (fullname
.get ()).release ();
1222 if (ps
->fullname
== NULL
)
1223 ps
->fullname
= fullname
.release ();
1227 return ps
->fullname
;
1230 /* For all symbols, s, in BLOCK that are in DOMAIN and match NAME
1231 according to the function MATCH, call CALLBACK(BLOCK, s, DATA).
1232 BLOCK is assumed to come from OBJFILE. Returns 1 iff CALLBACK
1233 ever returns non-zero, and otherwise returns 0. */
1236 map_block (const char *name
, domain_enum domain
, struct objfile
*objfile
,
1237 struct block
*block
,
1238 int (*callback
) (struct block
*, struct symbol
*, void *),
1239 void *data
, symbol_name_match_type match
)
1241 struct block_iterator iter
;
1244 lookup_name_info
lookup_name (name
, match
);
1246 for (sym
= block_iter_match_first (block
, lookup_name
, &iter
);
1248 sym
= block_iter_match_next (lookup_name
, &iter
))
1250 if (symbol_matches_domain (SYMBOL_LANGUAGE (sym
),
1251 SYMBOL_DOMAIN (sym
), domain
))
1253 if (callback (block
, sym
, data
))
1261 /* Psymtab version of map_matching_symbols. See its definition in
1262 the definition of quick_symbol_functions in symfile.h. */
1265 psym_map_matching_symbols (struct objfile
*objfile
,
1266 const char *name
, domain_enum domain
,
1268 int (*callback
) (struct block
*,
1269 struct symbol
*, void *),
1271 symbol_name_match_type match
,
1272 symbol_compare_ftype
*ordered_compare
)
1274 const int block_kind
= global
? GLOBAL_BLOCK
: STATIC_BLOCK
;
1275 struct partial_symtab
*ps
;
1277 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile
, ps
)
1281 || match_partial_symbol (objfile
, ps
, global
, name
, domain
, match
,
1284 struct compunit_symtab
*cust
= psymtab_to_symtab (objfile
, ps
);
1285 struct block
*block
;
1289 block
= BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust
), block_kind
);
1290 if (map_block (name
, domain
, objfile
, block
,
1291 callback
, data
, match
))
1293 if (callback (block
, NULL
, data
))
1299 /* A helper for psym_expand_symtabs_matching that handles searching
1300 included psymtabs. This returns true if a symbol is found, and
1301 false otherwise. It also updates the 'searched_flag' on the
1302 various psymtabs that it searches. */
1305 recursively_search_psymtabs
1306 (struct partial_symtab
*ps
, struct objfile
*objfile
, enum search_domain domain
,
1307 const lookup_name_info
&lookup_name
,
1308 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> sym_matcher
)
1311 enum psymtab_search_status result
= PST_SEARCHED_AND_NOT_FOUND
;
1314 if (ps
->searched_flag
!= PST_NOT_SEARCHED
)
1315 return ps
->searched_flag
== PST_SEARCHED_AND_FOUND
;
1317 /* Recurse into shared psymtabs first, because they may have already
1318 been searched, and this could save some time. */
1319 for (i
= 0; i
< ps
->number_of_dependencies
; ++i
)
1323 /* Skip non-shared dependencies, these are handled elsewhere. */
1324 if (ps
->dependencies
[i
]->user
== NULL
)
1327 r
= recursively_search_psymtabs (ps
->dependencies
[i
],
1328 objfile
, domain
, lookup_name
,
1332 ps
->searched_flag
= PST_SEARCHED_AND_FOUND
;
1337 partial_symbol
**gbound
1338 = objfile
->global_psymbols
.data () + ps
->globals_offset
+ ps
->n_global_syms
;
1339 partial_symbol
**sbound
1340 = objfile
->static_psymbols
.data () + ps
->statics_offset
+ ps
->n_static_syms
;
1341 partial_symbol
**bound
= gbound
;
1343 /* Go through all of the symbols stored in a partial
1344 symtab in one loop. */
1345 partial_symbol
**psym
= objfile
->global_psymbols
.data () + ps
->globals_offset
;
1350 if (bound
== gbound
&& ps
->n_static_syms
!= 0)
1352 psym
= objfile
->static_psymbols
.data () + ps
->statics_offset
;
1363 if ((domain
== ALL_DOMAIN
1364 || (domain
== VARIABLES_DOMAIN
1365 && (*psym
)->aclass
!= LOC_TYPEDEF
1366 && (*psym
)->aclass
!= LOC_BLOCK
)
1367 || (domain
== FUNCTIONS_DOMAIN
1368 && (*psym
)->aclass
== LOC_BLOCK
)
1369 || (domain
== TYPES_DOMAIN
1370 && (*psym
)->aclass
== LOC_TYPEDEF
))
1371 && psymbol_name_matches (*psym
, lookup_name
)
1372 && (sym_matcher
== NULL
|| sym_matcher (symbol_search_name (*psym
))))
1374 /* Found a match, so notify our caller. */
1375 result
= PST_SEARCHED_AND_FOUND
;
1382 ps
->searched_flag
= result
;
1383 return result
== PST_SEARCHED_AND_FOUND
;
1386 /* Psymtab version of expand_symtabs_matching. See its definition in
1387 the definition of quick_symbol_functions in symfile.h. */
1390 psym_expand_symtabs_matching
1391 (struct objfile
*objfile
,
1392 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
1393 const lookup_name_info
&lookup_name_in
,
1394 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
1395 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
1396 enum search_domain domain
)
1398 struct partial_symtab
*ps
;
1400 lookup_name_info lookup_name
= lookup_name_in
.make_ignore_params ();
1402 /* Clear the search flags. */
1403 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile
, ps
)
1405 ps
->searched_flag
= PST_NOT_SEARCHED
;
1408 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile
, ps
)
1415 /* We skip shared psymtabs because file-matching doesn't apply
1416 to them; but we search them later in the loop. */
1417 if (ps
->user
!= NULL
)
1427 match
= file_matcher (ps
->filename
, false);
1430 /* Before we invoke realpath, which can get expensive when many
1431 files are involved, do a quick comparison of the basenames. */
1432 if (basenames_may_differ
1433 || file_matcher (lbasename (ps
->filename
), true))
1434 match
= file_matcher (psymtab_to_fullname (ps
), false);
1440 if (recursively_search_psymtabs (ps
, objfile
, domain
,
1441 lookup_name
, symbol_matcher
))
1443 struct compunit_symtab
*symtab
=
1444 psymtab_to_symtab (objfile
, ps
);
1446 if (expansion_notify
!= NULL
)
1447 expansion_notify (symtab
);
1452 /* Psymtab version of has_symbols. See its definition in
1453 the definition of quick_symbol_functions in symfile.h. */
1456 psym_has_symbols (struct objfile
*objfile
)
1458 return objfile
->psymtabs
!= NULL
;
1461 /* Helper function for psym_find_compunit_symtab_by_address that fills
1462 in psymbol_map for a given range of psymbols. */
1465 psym_fill_psymbol_map (struct objfile
*objfile
,
1466 struct partial_symtab
*psymtab
,
1467 std::set
<CORE_ADDR
> *seen_addrs
,
1468 const std::vector
<partial_symbol
*> &symbols
,
1472 for (int i
= 0; i
< length
; ++i
)
1474 struct partial_symbol
*psym
= symbols
[start
+ i
];
1476 if (psym
->aclass
== LOC_STATIC
)
1478 CORE_ADDR addr
= psym
->value
.address
;
1479 if (seen_addrs
->find (addr
) == seen_addrs
->end ())
1481 seen_addrs
->insert (addr
);
1482 objfile
->psymbol_map
.emplace_back (addr
, psymtab
);
1488 /* See find_compunit_symtab_by_address in quick_symbol_functions, in
1491 static compunit_symtab
*
1492 psym_find_compunit_symtab_by_address (struct objfile
*objfile
,
1495 if (objfile
->psymbol_map
.empty ())
1497 struct partial_symtab
*pst
;
1499 std::set
<CORE_ADDR
> seen_addrs
;
1501 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile
, pst
)
1503 psym_fill_psymbol_map (objfile
, pst
,
1505 objfile
->global_psymbols
,
1506 pst
->globals_offset
,
1507 pst
->n_global_syms
);
1508 psym_fill_psymbol_map (objfile
, pst
,
1510 objfile
->static_psymbols
,
1511 pst
->statics_offset
,
1512 pst
->n_static_syms
);
1515 objfile
->psymbol_map
.shrink_to_fit ();
1517 std::sort (objfile
->psymbol_map
.begin (), objfile
->psymbol_map
.end (),
1518 [] (const std::pair
<CORE_ADDR
, partial_symtab
*> &a
,
1519 const std::pair
<CORE_ADDR
, partial_symtab
*> &b
)
1521 return a
.first
< b
.first
;
1525 auto iter
= std::lower_bound
1526 (objfile
->psymbol_map
.begin (), objfile
->psymbol_map
.end (), address
,
1527 [] (const std::pair
<CORE_ADDR
, partial_symtab
*> &a
,
1533 if (iter
== objfile
->psymbol_map
.end () || iter
->first
!= address
)
1536 return psymtab_to_symtab (objfile
, iter
->second
);
1539 const struct quick_symbol_functions psym_functions
=
1542 psym_find_last_source_symtab
,
1543 psym_forget_cached_source_info
,
1544 psym_map_symtabs_matching_filename
,
1549 psym_expand_symtabs_for_function
,
1550 psym_expand_all_symtabs
,
1551 psym_expand_symtabs_with_fullname
,
1552 psym_map_matching_symbols
,
1553 psym_expand_symtabs_matching
,
1554 psym_find_pc_sect_compunit_symtab
,
1555 psym_find_compunit_symtab_by_address
,
1556 psym_map_symbol_filenames
1562 sort_pst_symbols (struct objfile
*objfile
, struct partial_symtab
*pst
)
1564 /* Sort the global list; don't sort the static list. */
1565 auto begin
= objfile
->global_psymbols
.begin ();
1566 std::advance (begin
, pst
->globals_offset
);
1568 /* The psymbols for this partial_symtab are currently at the end of the
1570 auto end
= objfile
->global_psymbols
.end ();
1572 std::sort (begin
, end
, [] (partial_symbol
*s1
, partial_symbol
*s2
)
1574 return strcmp_iw_ordered (symbol_search_name (s1
),
1575 symbol_search_name (s2
)) < 0;
1579 /* Allocate and partially fill a partial symtab. It will be
1580 completely filled at the end of the symbol list.
1582 FILENAME is the name of the symbol-file we are reading from. */
1584 struct partial_symtab
*
1585 start_psymtab_common (struct objfile
*objfile
,
1586 const char *filename
,
1588 std::vector
<partial_symbol
*> &global_psymbols
,
1589 std::vector
<partial_symbol
*> &static_psymbols
)
1591 struct partial_symtab
*psymtab
;
1593 psymtab
= allocate_psymtab (filename
, objfile
);
1594 psymtab
->textlow
= textlow
;
1595 psymtab
->texthigh
= psymtab
->textlow
; /* default */
1596 psymtab
->globals_offset
= global_psymbols
.size ();
1597 psymtab
->statics_offset
= static_psymbols
.size ();
1601 /* Perform "finishing up" operations of a partial symtab. */
1604 end_psymtab_common (struct objfile
*objfile
, struct partial_symtab
*pst
)
1606 pst
->n_global_syms
= objfile
->global_psymbols
.size () - pst
->globals_offset
;
1607 pst
->n_static_syms
= objfile
->static_psymbols
.size () - pst
->statics_offset
;
1609 sort_pst_symbols (objfile
, pst
);
1612 /* Calculate a hash code for the given partial symbol. The hash is
1613 calculated using the symbol's value, language, domain, class
1614 and name. These are the values which are set by
1615 add_psymbol_to_bcache. */
1617 static unsigned long
1618 psymbol_hash (const void *addr
, int length
)
1620 unsigned long h
= 0;
1621 struct partial_symbol
*psymbol
= (struct partial_symbol
*) addr
;
1622 unsigned int lang
= psymbol
->language
;
1623 unsigned int domain
= psymbol
->domain
;
1624 unsigned int theclass
= psymbol
->aclass
;
1626 h
= hash_continue (&psymbol
->value
, sizeof (psymbol
->value
), h
);
1627 h
= hash_continue (&lang
, sizeof (unsigned int), h
);
1628 h
= hash_continue (&domain
, sizeof (unsigned int), h
);
1629 h
= hash_continue (&theclass
, sizeof (unsigned int), h
);
1630 /* Note that psymbol names are interned via symbol_set_names, so
1631 there's no need to hash the contents of the name here. */
1632 h
= hash_continue (&psymbol
->name
,
1633 sizeof (psymbol
->name
), h
);
1638 /* Returns true if the symbol at addr1 equals the symbol at addr2.
1639 For the comparison this function uses a symbols value,
1640 language, domain, class and name. */
1643 psymbol_compare (const void *addr1
, const void *addr2
, int length
)
1645 struct partial_symbol
*sym1
= (struct partial_symbol
*) addr1
;
1646 struct partial_symbol
*sym2
= (struct partial_symbol
*) addr2
;
1648 return (memcmp (&sym1
->value
, &sym2
->value
,
1649 sizeof (sym1
->value
)) == 0
1650 && sym1
->language
== sym2
->language
1651 && sym1
->domain
== sym2
->domain
1652 && sym1
->aclass
== sym2
->aclass
1653 /* Note that psymbol names are interned via
1654 symbol_set_names, so there's no need to compare the
1655 contents of the name here. */
1656 && sym1
->name
== sym2
->name
);
1659 /* Initialize a partial symbol bcache. */
1661 struct psymbol_bcache
*
1662 psymbol_bcache_init (void)
1664 struct psymbol_bcache
*bcache
= XCNEW (struct psymbol_bcache
);
1666 bcache
->bcache
= bcache_xmalloc (psymbol_hash
, psymbol_compare
);
1670 /* Free a partial symbol bcache. */
1673 psymbol_bcache_free (struct psymbol_bcache
*bcache
)
1678 bcache_xfree (bcache
->bcache
);
1682 /* Return the internal bcache of the psymbol_bcache BCACHE. */
1685 psymbol_bcache_get_bcache (struct psymbol_bcache
*bcache
)
1687 return bcache
->bcache
;
1690 /* Find a copy of the SYM in BCACHE. If BCACHE has never seen this
1691 symbol before, add a copy to BCACHE. In either case, return a pointer
1692 to BCACHE's copy of the symbol. If optional ADDED is not NULL, return
1693 1 in case of new entry or 0 if returning an old entry. */
1695 static struct partial_symbol
*
1696 psymbol_bcache_full (struct partial_symbol
*sym
,
1697 struct psymbol_bcache
*bcache
,
1700 return ((struct partial_symbol
*)
1701 bcache_full (sym
, sizeof (struct partial_symbol
), bcache
->bcache
,
1705 /* Helper function, initialises partial symbol structure and stashes
1706 it into objfile's bcache. Note that our caching mechanism will
1707 use all fields of struct partial_symbol to determine hash value of the
1708 structure. In other words, having two symbols with the same name but
1709 different domain (or address) is possible and correct. */
1711 static struct partial_symbol
*
1712 add_psymbol_to_bcache (const char *name
, int namelength
, int copy_name
,
1714 enum address_class theclass
,
1716 enum language language
, struct objfile
*objfile
,
1719 struct partial_symbol psymbol
;
1721 psymbol
.value
.address
= coreaddr
;
1722 psymbol
.section
= -1;
1723 psymbol
.domain
= domain
;
1724 psymbol
.aclass
= theclass
;
1726 memset (&psymbol
.language_specific
, 0, sizeof (psymbol
.language_specific
));
1727 psymbol
.ada_mangled
= 0;
1728 symbol_set_language (&psymbol
, language
, &objfile
->objfile_obstack
);
1729 symbol_set_names (&psymbol
, name
, namelength
, copy_name
, objfile
);
1731 /* Stash the partial symbol away in the cache. */
1732 return psymbol_bcache_full (&psymbol
, objfile
->psymbol_cache
, added
);
1735 /* Helper function, adds partial symbol to the given partial symbol list. */
1738 append_psymbol_to_list (std::vector
<partial_symbol
*> *list
,
1739 struct partial_symbol
*psym
,
1740 struct objfile
*objfile
)
1742 list
->push_back (psym
);
1743 OBJSTAT (objfile
, n_psyms
++);
1746 /* Add a symbol with a long value to a psymtab.
1747 Since one arg is a struct, we pass in a ptr and deref it (sigh).
1748 The only value we need to store for psyms is an address.
1749 For all other psyms pass zero for COREADDR.
1750 Return the partial symbol that has been added. */
1753 add_psymbol_to_list (const char *name
, int namelength
, int copy_name
,
1755 enum address_class theclass
,
1756 std::vector
<partial_symbol
*> *list
,
1758 enum language language
, struct objfile
*objfile
)
1760 struct partial_symbol
*psym
;
1764 /* Stash the partial symbol away in the cache. */
1765 psym
= add_psymbol_to_bcache (name
, namelength
, copy_name
, domain
, theclass
,
1766 coreaddr
, language
, objfile
, &added
);
1768 /* Do not duplicate global partial symbols. */
1769 if (list
== &objfile
->global_psymbols
1773 /* Save pointer to partial symbol in psymtab, growing symtab if needed. */
1774 append_psymbol_to_list (list
, psym
, objfile
);
1777 /* Initialize storage for partial symbols. */
1780 init_psymbol_list (struct objfile
*objfile
, int total_symbols
)
1782 /* Free any previously allocated psymbol lists. */
1783 objfile
->global_psymbols
.clear ();
1784 objfile
->static_psymbols
.clear ();
1786 /* Current best guess is that approximately a twentieth
1787 of the total symbols (in a debugging file) are global or static
1788 oriented symbols, then multiply that by slop factor of two. */
1789 objfile
->global_psymbols
.reserve (total_symbols
/ 10);
1790 objfile
->static_psymbols
.reserve (total_symbols
/ 10);
1793 struct partial_symtab
*
1794 allocate_psymtab (const char *filename
, struct objfile
*objfile
)
1796 struct partial_symtab
*psymtab
;
1798 if (objfile
->free_psymtabs
)
1800 psymtab
= objfile
->free_psymtabs
;
1801 objfile
->free_psymtabs
= psymtab
->next
;
1804 psymtab
= XOBNEW (&objfile
->objfile_obstack
, partial_symtab
);
1806 memset (psymtab
, 0, sizeof (struct partial_symtab
));
1808 = (const char *) bcache (filename
, strlen (filename
) + 1,
1809 objfile
->per_bfd
->filename_cache
);
1810 psymtab
->compunit_symtab
= NULL
;
1812 /* Prepend it to the psymtab list for the objfile it belongs to.
1813 Psymtabs are searched in most recent inserted -> least recent
1816 psymtab
->next
= objfile
->psymtabs
;
1817 objfile
->psymtabs
= psymtab
;
1819 if (symtab_create_debug
)
1821 /* Be a bit clever with debugging messages, and don't print objfile
1822 every time, only when it changes. */
1823 static char *last_objfile_name
= NULL
;
1825 if (last_objfile_name
== NULL
1826 || strcmp (last_objfile_name
, objfile_name (objfile
)) != 0)
1828 xfree (last_objfile_name
);
1829 last_objfile_name
= xstrdup (objfile_name (objfile
));
1830 fprintf_unfiltered (gdb_stdlog
,
1831 "Creating one or more psymtabs for objfile %s ...\n",
1834 fprintf_unfiltered (gdb_stdlog
,
1835 "Created psymtab %s for module %s.\n",
1836 host_address_to_string (psymtab
), filename
);
1843 discard_psymtab (struct objfile
*objfile
, struct partial_symtab
*pst
)
1845 struct partial_symtab
**prev_pst
;
1848 Empty psymtabs happen as a result of header files which don't
1849 have any symbols in them. There can be a lot of them. But this
1850 check is wrong, in that a psymtab with N_SLINE entries but
1851 nothing else is not empty, but we don't realize that. Fixing
1852 that without slowing things down might be tricky. */
1854 /* First, snip it out of the psymtab chain. */
1856 prev_pst
= &(objfile
->psymtabs
);
1857 while ((*prev_pst
) != pst
)
1858 prev_pst
= &((*prev_pst
)->next
);
1859 (*prev_pst
) = pst
->next
;
1861 /* Next, put it on a free list for recycling. */
1863 pst
->next
= objfile
->free_psymtabs
;
1864 objfile
->free_psymtabs
= pst
;
1869 /* We need to pass a couple of items to the addrmap_foreach function,
1872 struct dump_psymtab_addrmap_data
1874 struct objfile
*objfile
;
1875 struct partial_symtab
*psymtab
;
1876 struct ui_file
*outfile
;
1878 /* Non-zero if the previously printed addrmap entry was for PSYMTAB.
1879 If so, we want to print the next one as well (since the next addrmap
1880 entry defines the end of the range). */
1881 int previous_matched
;
1884 /* Helper function for dump_psymtab_addrmap to print an addrmap entry. */
1887 dump_psymtab_addrmap_1 (void *datap
, CORE_ADDR start_addr
, void *obj
)
1889 struct dump_psymtab_addrmap_data
*data
1890 = (struct dump_psymtab_addrmap_data
*) datap
;
1891 struct gdbarch
*gdbarch
= get_objfile_arch (data
->objfile
);
1892 struct partial_symtab
*addrmap_psymtab
= (struct partial_symtab
*) obj
;
1893 const char *psymtab_address_or_end
= NULL
;
1897 if (data
->psymtab
== NULL
1898 || data
->psymtab
== addrmap_psymtab
)
1899 psymtab_address_or_end
= host_address_to_string (addrmap_psymtab
);
1900 else if (data
->previous_matched
)
1901 psymtab_address_or_end
= "<ends here>";
1903 if (data
->psymtab
== NULL
1904 || data
->psymtab
== addrmap_psymtab
1905 || data
->previous_matched
)
1907 fprintf_filtered (data
->outfile
, " %s%s %s\n",
1908 data
->psymtab
!= NULL
? " " : "",
1909 paddress (gdbarch
, start_addr
),
1910 psymtab_address_or_end
);
1913 data
->previous_matched
= (data
->psymtab
== NULL
1914 || data
->psymtab
== addrmap_psymtab
);
1919 /* Helper function for maintenance_print_psymbols to print the addrmap
1920 of PSYMTAB. If PSYMTAB is NULL print the entire addrmap. */
1923 dump_psymtab_addrmap (struct objfile
*objfile
, struct partial_symtab
*psymtab
,
1924 struct ui_file
*outfile
)
1926 struct dump_psymtab_addrmap_data addrmap_dump_data
;
1928 if ((psymtab
== NULL
1929 || psymtab
->psymtabs_addrmap_supported
)
1930 && objfile
->psymtabs_addrmap
!= NULL
)
1932 addrmap_dump_data
.objfile
= objfile
;
1933 addrmap_dump_data
.psymtab
= psymtab
;
1934 addrmap_dump_data
.outfile
= outfile
;
1935 addrmap_dump_data
.previous_matched
= 0;
1936 fprintf_filtered (outfile
, "%sddress map:\n",
1937 psymtab
== NULL
? "Entire a" : " A");
1938 addrmap_foreach (objfile
->psymtabs_addrmap
, dump_psymtab_addrmap_1
,
1939 &addrmap_dump_data
);
1944 maintenance_print_psymbols (const char *args
, int from_tty
)
1946 struct ui_file
*outfile
= gdb_stdout
;
1947 char *address_arg
= NULL
, *source_arg
= NULL
, *objfile_arg
= NULL
;
1948 struct objfile
*objfile
;
1949 struct partial_symtab
*ps
;
1950 int i
, outfile_idx
, found
;
1952 struct obj_section
*section
= NULL
;
1956 gdb_argv
argv (args
);
1958 for (i
= 0; argv
!= NULL
&& argv
[i
] != NULL
; ++i
)
1960 if (strcmp (argv
[i
], "-pc") == 0)
1962 if (argv
[i
+ 1] == NULL
)
1963 error (_("Missing pc value"));
1964 address_arg
= argv
[++i
];
1966 else if (strcmp (argv
[i
], "-source") == 0)
1968 if (argv
[i
+ 1] == NULL
)
1969 error (_("Missing source file"));
1970 source_arg
= argv
[++i
];
1972 else if (strcmp (argv
[i
], "-objfile") == 0)
1974 if (argv
[i
+ 1] == NULL
)
1975 error (_("Missing objfile name"));
1976 objfile_arg
= argv
[++i
];
1978 else if (strcmp (argv
[i
], "--") == 0)
1980 /* End of options. */
1984 else if (argv
[i
][0] == '-')
1986 /* Future proofing: Don't allow OUTFILE to begin with "-". */
1987 error (_("Unknown option: %s"), argv
[i
]);
1994 if (address_arg
!= NULL
&& source_arg
!= NULL
)
1995 error (_("Must specify at most one of -pc and -source"));
1997 stdio_file arg_outfile
;
1999 if (argv
!= NULL
&& argv
[outfile_idx
] != NULL
)
2001 if (argv
[outfile_idx
+ 1] != NULL
)
2002 error (_("Junk at end of command"));
2003 gdb::unique_xmalloc_ptr
<char> outfile_name
2004 (tilde_expand (argv
[outfile_idx
]));
2005 if (!arg_outfile
.open (outfile_name
.get (), FOPEN_WT
))
2006 perror_with_name (outfile_name
.get ());
2007 outfile
= &arg_outfile
;
2010 if (address_arg
!= NULL
)
2012 pc
= parse_and_eval_address (address_arg
);
2013 /* If we fail to find a section, that's ok, try the lookup anyway. */
2014 section
= find_pc_section (pc
);
2018 ALL_OBJFILES (objfile
)
2020 int printed_objfile_header
= 0;
2021 int print_for_objfile
= 1;
2024 if (objfile_arg
!= NULL
)
2026 = compare_filenames_for_search (objfile_name (objfile
),
2028 if (!print_for_objfile
)
2031 if (address_arg
!= NULL
)
2033 struct bound_minimal_symbol msymbol
= { NULL
, NULL
};
2035 /* We don't assume each pc has a unique objfile (this is for
2037 ps
= find_pc_sect_psymtab (objfile
, pc
, section
, msymbol
);
2040 if (!printed_objfile_header
)
2042 outfile
->printf ("\nPartial symtabs for objfile %s\n",
2043 objfile_name (objfile
));
2044 printed_objfile_header
= 1;
2046 dump_psymtab (objfile
, ps
, outfile
);
2047 dump_psymtab_addrmap (objfile
, ps
, outfile
);
2053 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile
, ps
)
2055 int print_for_source
= 0;
2058 if (source_arg
!= NULL
)
2061 = compare_filenames_for_search (ps
->filename
, source_arg
);
2064 if (source_arg
== NULL
2065 || print_for_source
)
2067 if (!printed_objfile_header
)
2069 outfile
->printf ("\nPartial symtabs for objfile %s\n",
2070 objfile_name (objfile
));
2071 printed_objfile_header
= 1;
2073 dump_psymtab (objfile
, ps
, outfile
);
2074 dump_psymtab_addrmap (objfile
, ps
, outfile
);
2079 /* If we're printing all the objfile's symbols dump the full addrmap. */
2081 if (address_arg
== NULL
2082 && source_arg
== NULL
2083 && objfile
->psymtabs_addrmap
!= NULL
)
2085 outfile
->puts ("\n");
2086 dump_psymtab_addrmap (objfile
, NULL
, outfile
);
2092 if (address_arg
!= NULL
)
2093 error (_("No partial symtab for address: %s"), address_arg
);
2094 if (source_arg
!= NULL
)
2095 error (_("No partial symtab for source file: %s"), source_arg
);
2099 /* List all the partial symbol tables whose names match REGEXP (optional). */
2102 maintenance_info_psymtabs (const char *regexp
, int from_tty
)
2104 struct program_space
*pspace
;
2105 struct objfile
*objfile
;
2110 ALL_PSPACES (pspace
)
2111 ALL_PSPACE_OBJFILES (pspace
, objfile
)
2113 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
2114 struct partial_symtab
*psymtab
;
2116 /* We don't want to print anything for this objfile until we
2117 actually find a symtab whose name matches. */
2118 int printed_objfile_start
= 0;
2120 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile
, psymtab
)
2125 || re_exec (psymtab
->filename
))
2127 if (! printed_objfile_start
)
2129 printf_filtered ("{ objfile %s ", objfile_name (objfile
));
2131 printf_filtered ("((struct objfile *) %s)\n",
2132 host_address_to_string (objfile
));
2133 printed_objfile_start
= 1;
2136 printf_filtered (" { psymtab %s ", psymtab
->filename
);
2138 printf_filtered ("((struct partial_symtab *) %s)\n",
2139 host_address_to_string (psymtab
));
2141 printf_filtered (" readin %s\n",
2142 psymtab
->readin
? "yes" : "no");
2143 printf_filtered (" fullname %s\n",
2145 ? psymtab
->fullname
: "(null)");
2146 printf_filtered (" text addresses ");
2147 fputs_filtered (paddress (gdbarch
, psymtab
->textlow
),
2149 printf_filtered (" -- ");
2150 fputs_filtered (paddress (gdbarch
, psymtab
->texthigh
),
2152 printf_filtered ("\n");
2153 printf_filtered (" psymtabs_addrmap_supported %s\n",
2154 (psymtab
->psymtabs_addrmap_supported
2156 printf_filtered (" globals ");
2157 if (psymtab
->n_global_syms
)
2159 auto p
= &objfile
->global_psymbols
[psymtab
->globals_offset
];
2161 printf_filtered ("(* (struct partial_symbol **) %s @ %d)\n",
2162 host_address_to_string (p
),
2163 psymtab
->n_global_syms
);
2166 printf_filtered ("(none)\n");
2167 printf_filtered (" statics ");
2168 if (psymtab
->n_static_syms
)
2170 auto p
= &objfile
->static_psymbols
[psymtab
->statics_offset
];
2172 printf_filtered ("(* (struct partial_symbol **) %s @ %d)\n",
2173 host_address_to_string (p
),
2174 psymtab
->n_static_syms
);
2177 printf_filtered ("(none)\n");
2178 printf_filtered (" dependencies ");
2179 if (psymtab
->number_of_dependencies
)
2183 printf_filtered ("{\n");
2184 for (i
= 0; i
< psymtab
->number_of_dependencies
; i
++)
2186 struct partial_symtab
*dep
= psymtab
->dependencies
[i
];
2188 /* Note the string concatenation there --- no comma. */
2189 printf_filtered (" psymtab %s "
2190 "((struct partial_symtab *) %s)\n",
2192 host_address_to_string (dep
));
2194 printf_filtered (" }\n");
2197 printf_filtered ("(none)\n");
2198 printf_filtered (" }\n");
2202 if (printed_objfile_start
)
2203 printf_filtered ("}\n");
2207 /* Check consistency of currently expanded psymtabs vs symtabs. */
2210 maintenance_check_psymtabs (const char *ignore
, int from_tty
)
2213 struct compunit_symtab
*cust
= NULL
;
2214 struct partial_symtab
*ps
;
2215 const struct blockvector
*bv
;
2216 struct objfile
*objfile
;
2220 ALL_PSYMTABS (objfile
, ps
)
2222 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
2224 /* We don't call psymtab_to_symtab here because that may cause symtab
2225 expansion. When debugging a problem it helps if checkers leave
2226 things unchanged. */
2227 cust
= ps
->compunit_symtab
;
2229 /* First do some checks that don't require the associated symtab. */
2230 if (ps
->texthigh
< ps
->textlow
)
2232 printf_filtered ("Psymtab ");
2233 puts_filtered (ps
->filename
);
2234 printf_filtered (" covers bad range ");
2235 fputs_filtered (paddress (gdbarch
, ps
->textlow
), gdb_stdout
);
2236 printf_filtered (" - ");
2237 fputs_filtered (paddress (gdbarch
, ps
->texthigh
), gdb_stdout
);
2238 printf_filtered ("\n");
2242 /* Now do checks requiring the associated symtab. */
2245 bv
= COMPUNIT_BLOCKVECTOR (cust
);
2246 b
= BLOCKVECTOR_BLOCK (bv
, STATIC_BLOCK
);
2247 partial_symbol
**psym
= &objfile
->static_psymbols
[ps
->statics_offset
];
2248 length
= ps
->n_static_syms
;
2251 sym
= block_lookup_symbol (b
, symbol_search_name (*psym
),
2252 symbol_name_match_type::SEARCH_NAME
,
2256 printf_filtered ("Static symbol `");
2257 puts_filtered ((*psym
)->name
);
2258 printf_filtered ("' only found in ");
2259 puts_filtered (ps
->filename
);
2260 printf_filtered (" psymtab\n");
2264 b
= BLOCKVECTOR_BLOCK (bv
, GLOBAL_BLOCK
);
2265 psym
= &objfile
->global_psymbols
[ps
->globals_offset
];
2266 length
= ps
->n_global_syms
;
2269 sym
= block_lookup_symbol (b
, symbol_search_name (*psym
),
2270 symbol_name_match_type::SEARCH_NAME
,
2274 printf_filtered ("Global symbol `");
2275 puts_filtered ((*psym
)->name
);
2276 printf_filtered ("' only found in ");
2277 puts_filtered (ps
->filename
);
2278 printf_filtered (" psymtab\n");
2282 if (ps
->texthigh
!= 0
2283 && (ps
->textlow
< BLOCK_START (b
) || ps
->texthigh
> BLOCK_END (b
)))
2285 printf_filtered ("Psymtab ");
2286 puts_filtered (ps
->filename
);
2287 printf_filtered (" covers ");
2288 fputs_filtered (paddress (gdbarch
, ps
->textlow
), gdb_stdout
);
2289 printf_filtered (" - ");
2290 fputs_filtered (paddress (gdbarch
, ps
->texthigh
), gdb_stdout
);
2291 printf_filtered (" but symtab covers only ");
2292 fputs_filtered (paddress (gdbarch
, BLOCK_START (b
)), gdb_stdout
);
2293 printf_filtered (" - ");
2294 fputs_filtered (paddress (gdbarch
, BLOCK_END (b
)), gdb_stdout
);
2295 printf_filtered ("\n");
2301 _initialize_psymtab (void)
2303 add_cmd ("psymbols", class_maintenance
, maintenance_print_psymbols
, _("\
2304 Print dump of current partial symbol definitions.\n\
2305 Usage: mt print psymbols [-objfile objfile] [-pc address] [--] [outfile]\n\
2306 mt print psymbols [-objfile objfile] [-source source] [--] [outfile]\n\
2307 Entries in the partial symbol table are dumped to file OUTFILE,\n\
2308 or the terminal if OUTFILE is unspecified.\n\
2309 If ADDRESS is provided, dump only the file for that address.\n\
2310 If SOURCE is provided, dump only that file's symbols.\n\
2311 If OBJFILE is provided, dump only that file's minimal symbols."),
2312 &maintenanceprintlist
);
2314 add_cmd ("psymtabs", class_maintenance
, maintenance_info_psymtabs
, _("\
2315 List the partial symbol tables for all object files.\n\
2316 This does not include information about individual partial symbols,\n\
2317 just the symbol table structures themselves."),
2318 &maintenanceinfolist
);
2320 add_cmd ("check-psymtabs", class_maintenance
, maintenance_check_psymtabs
,
2322 Check consistency of currently expanded psymtabs versus symtabs."),