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