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