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