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