]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/psymtab.c
darwin-nat: Remove gdb.h include
[thirdparty/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,
2edb89d3
JK
49 symbol_compare_ftype *,
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
b11896a5 513 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
ccefe4c4 514 {
5c80ed9d
TT
515 if (!ps->readin && lookup_partial_symbol (objfile, ps, name,
516 psymtab_index, domain))
c00f8484 517 {
b2e2f908 518 struct symbol *sym, *with_opaque = NULL;
43f3e411 519 struct compunit_symtab *stab = psymtab_to_symtab (objfile, ps);
f194fefb
DE
520 /* Note: While psymtab_to_symtab can return NULL if the partial symtab
521 is empty, we can assume it won't here because lookup_partial_symbol
522 succeeded. */
43f3e411 523 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
f194fefb 524 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
c00f8484 525
b2e2f908
DE
526 sym = block_find_symbol (block, name, domain,
527 block_find_non_opaque_type_preferred,
528 &with_opaque);
529
c00f8484 530 /* Some caution must be observed with overloaded functions
b2e2f908 531 and methods, since the index will not contain any overload
c00f8484 532 information (but NAME might contain it). */
bfb05775 533
b2e2f908 534 if (sym != NULL
a778f165 535 && SYMBOL_MATCHES_SEARCH_NAME (sym, name))
b2e2f908
DE
536 return stab;
537 if (with_opaque != NULL
a778f165 538 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, name))
b2e2f908 539 stab_best = stab;
c00f8484
KS
540
541 /* Keep looking through other psymtabs. */
542 }
ccefe4c4
TT
543 }
544
bfb05775 545 return stab_best;
ccefe4c4
TT
546}
547
40658b94
PH
548/* Look in PST for a symbol in DOMAIN whose name matches NAME. Search
549 the global block of PST if GLOBAL, and otherwise the static block.
550 MATCH is the comparison operation that returns true iff MATCH (s,
551 NAME), where s is a SYMBOL_SEARCH_NAME. If ORDERED_COMPARE is
552 non-null, the symbols in the block are assumed to be ordered
553 according to it (allowing binary search). It must be compatible
554 with MATCH. Returns the symbol, if found, and otherwise NULL. */
555
556static struct partial_symbol *
5c80ed9d
TT
557match_partial_symbol (struct objfile *objfile,
558 struct partial_symtab *pst, int global,
40658b94 559 const char *name, domain_enum domain,
2edb89d3
JK
560 symbol_compare_ftype *match,
561 symbol_compare_ftype *ordered_compare)
40658b94
PH
562{
563 struct partial_symbol **start, **psym;
564 struct partial_symbol **top, **real_top, **bottom, **center;
565 int length = (global ? pst->n_global_syms : pst->n_static_syms);
566 int do_linear_search = 1;
567
568 if (length == 0)
569 return NULL;
570 start = (global ?
af5bf4ad
SM
571 &objfile->global_psymbols[pst->globals_offset] :
572 &objfile->static_psymbols[pst->statics_offset]);
40658b94
PH
573
574 if (global && ordered_compare) /* Can use a binary search. */
575 {
576 do_linear_search = 0;
577
578 /* Binary search. This search is guaranteed to end with center
579 pointing at the earliest partial symbol whose name might be
580 correct. At that point *all* partial symbols with an
581 appropriate name will be checked against the correct
582 domain. */
583
584 bottom = start;
585 top = start + length - 1;
586 real_top = top;
587 while (top > bottom)
588 {
589 center = bottom + (top - bottom) / 2;
590 gdb_assert (center < top);
40658b94
PH
591 if (ordered_compare (SYMBOL_SEARCH_NAME (*center), name) >= 0)
592 top = center;
593 else
594 bottom = center + 1;
595 }
596 gdb_assert (top == bottom);
597
598 while (top <= real_top
599 && match (SYMBOL_SEARCH_NAME (*top), name) == 0)
600 {
4186eb54
KS
601 if (symbol_matches_domain (SYMBOL_LANGUAGE (*top),
602 SYMBOL_DOMAIN (*top), domain))
40658b94
PH
603 return *top;
604 top++;
605 }
606 }
607
608 /* Can't use a binary search or else we found during the binary search that
609 we should also do a linear search. */
610
611 if (do_linear_search)
612 {
613 for (psym = start; psym < start + length; psym++)
614 {
4186eb54
KS
615 if (symbol_matches_domain (SYMBOL_LANGUAGE (*psym),
616 SYMBOL_DOMAIN (*psym), domain)
40658b94
PH
617 && match (SYMBOL_SEARCH_NAME (*psym), name) == 0)
618 return *psym;
619 }
620 }
621
622 return NULL;
623}
624
c00f8484
KS
625/* Returns the name used to search psymtabs. Unlike symtabs, psymtabs do
626 not contain any method/function instance information (since this would
627 force reading type information while reading psymtabs). Therefore,
628 if NAME contains overload information, it must be stripped before searching
109483d9 629 psymtabs. */
c00f8484 630
56f37645 631static gdb::unique_xmalloc_ptr<char>
c00f8484
KS
632psymtab_search_name (const char *name)
633{
634 switch (current_language->la_language)
635 {
636 case language_cplus:
c00f8484 637 {
b2a919a8
DE
638 if (strchr (name, '('))
639 {
109483d9 640 gdb::unique_xmalloc_ptr<char> ret = cp_remove_params (name);
c00f8484 641
b2a919a8 642 if (ret)
109483d9 643 return ret;
b2a919a8 644 }
c00f8484
KS
645 }
646 break;
647
648 default:
649 break;
650 }
651
56f37645 652 return gdb::unique_xmalloc_ptr<char> (xstrdup (name));
c00f8484
KS
653}
654
ccefe4c4 655/* Look, in partial_symtab PST, for symbol whose natural name is NAME.
40658b94 656 Check the global symbols if GLOBAL, the static symbols if not. */
ccefe4c4 657
18430289 658static struct partial_symbol *
5c80ed9d
TT
659lookup_partial_symbol (struct objfile *objfile,
660 struct partial_symtab *pst, const char *name,
ccefe4c4
TT
661 int global, domain_enum domain)
662{
ccefe4c4
TT
663 struct partial_symbol **start, **psym;
664 struct partial_symbol **top, **real_top, **bottom, **center;
665 int length = (global ? pst->n_global_syms : pst->n_static_syms);
666 int do_linear_search = 1;
667
668 if (length == 0)
95cf5869 669 return NULL;
c00f8484 670
56f37645 671 gdb::unique_xmalloc_ptr<char> search_name = psymtab_search_name (name);
ccefe4c4 672 start = (global ?
af5bf4ad
SM
673 &objfile->global_psymbols[pst->globals_offset] :
674 &objfile->static_psymbols[pst->statics_offset]);
ccefe4c4 675
0df8b418 676 if (global) /* This means we can use a binary search. */
ccefe4c4
TT
677 {
678 do_linear_search = 0;
679
680 /* Binary search. This search is guaranteed to end with center
681 pointing at the earliest partial symbol whose name might be
682 correct. At that point *all* partial symbols with an
683 appropriate name will be checked against the correct
684 domain. */
685
686 bottom = start;
687 top = start + length - 1;
688 real_top = top;
689 while (top > bottom)
690 {
691 center = bottom + (top - bottom) / 2;
692 if (!(center < top))
3e43a32a
MS
693 internal_error (__FILE__, __LINE__,
694 _("failed internal consistency check"));
c00f8484 695 if (strcmp_iw_ordered (SYMBOL_SEARCH_NAME (*center),
56f37645 696 search_name.get ()) >= 0)
ccefe4c4
TT
697 {
698 top = center;
699 }
700 else
701 {
702 bottom = center + 1;
703 }
704 }
705 if (!(top == bottom))
3e43a32a
MS
706 internal_error (__FILE__, __LINE__,
707 _("failed internal consistency check"));
ccefe4c4 708
559a7a62
JK
709 /* For `case_sensitivity == case_sensitive_off' strcmp_iw_ordered will
710 search more exactly than what matches SYMBOL_MATCHES_SEARCH_NAME. */
56f37645
TT
711 while (top >= start && SYMBOL_MATCHES_SEARCH_NAME (*top,
712 search_name.get ()))
559a7a62
JK
713 top--;
714
715 /* Fixup to have a symbol which matches SYMBOL_MATCHES_SEARCH_NAME. */
716 top++;
717
56f37645
TT
718 while (top <= real_top
719 && SYMBOL_MATCHES_SEARCH_NAME (*top, search_name.get ()))
ccefe4c4 720 {
4186eb54
KS
721 if (symbol_matches_domain (SYMBOL_LANGUAGE (*top),
722 SYMBOL_DOMAIN (*top), domain))
56f37645 723 return *top;
ccefe4c4
TT
724 top++;
725 }
726 }
727
728 /* Can't use a binary search or else we found during the binary search that
40658b94 729 we should also do a linear search. */
ccefe4c4
TT
730
731 if (do_linear_search)
732 {
733 for (psym = start; psym < start + length; psym++)
734 {
4186eb54
KS
735 if (symbol_matches_domain (SYMBOL_LANGUAGE (*psym),
736 SYMBOL_DOMAIN (*psym), domain)
56f37645
TT
737 && SYMBOL_MATCHES_SEARCH_NAME (*psym, search_name.get ()))
738 return *psym;
ccefe4c4
TT
739 }
740 }
741
95cf5869 742 return NULL;
ccefe4c4
TT
743}
744
745/* Get the symbol table that corresponds to a partial_symtab.
f194fefb
DE
746 This is fast after the first time you do it.
747 The result will be NULL if the primary symtab has no symbols,
748 which can happen. Otherwise the result is the primary symtab
749 that contains PST. */
ccefe4c4 750
43f3e411 751static struct compunit_symtab *
5c80ed9d 752psymtab_to_symtab (struct objfile *objfile, struct partial_symtab *pst)
ccefe4c4 753{
9439a077
TT
754 /* If it is a shared psymtab, find an unshared psymtab that includes
755 it. Any such psymtab will do. */
756 while (pst->user != NULL)
757 pst = pst->user;
758
0df8b418 759 /* If it's been looked up before, return it. */
43f3e411
DE
760 if (pst->compunit_symtab)
761 return pst->compunit_symtab;
ccefe4c4
TT
762
763 /* If it has not yet been read in, read it. */
764 if (!pst->readin)
765 {
c83dd867 766 scoped_restore decrementer = increment_reading_symtab ();
ad3bbd48 767
257e7a09 768 (*pst->read_symtab) (pst, objfile);
ccefe4c4
TT
769 }
770
43f3e411 771 return pst->compunit_symtab;
ccefe4c4
TT
772}
773
95cf5869
DE
774/* Psymtab version of relocate. See its definition in
775 the definition of quick_symbol_functions in symfile.h. */
83827540 776
ccefe4c4 777static void
83827540
DE
778psym_relocate (struct objfile *objfile,
779 const struct section_offsets *new_offsets,
780 const struct section_offsets *delta)
ccefe4c4
TT
781{
782 struct partial_symbol **psym;
783 struct partial_symtab *p;
784
b11896a5 785 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, p)
ccefe4c4
TT
786 {
787 p->textlow += ANOFFSET (delta, SECT_OFF_TEXT (objfile));
788 p->texthigh += ANOFFSET (delta, SECT_OFF_TEXT (objfile));
789 }
790
af5bf4ad 791 for (partial_symbol *psym : objfile->global_psymbols)
ccefe4c4 792 {
af5bf4ad
SM
793 fixup_psymbol_section (psym, objfile);
794 if (SYMBOL_SECTION (psym) >= 0)
795 SYMBOL_VALUE_ADDRESS (psym) += ANOFFSET (delta, SYMBOL_SECTION (psym));
ccefe4c4 796 }
af5bf4ad 797 for (partial_symbol *psym : objfile->static_psymbols)
ccefe4c4 798 {
af5bf4ad
SM
799 fixup_psymbol_section (psym, objfile);
800 if (SYMBOL_SECTION (psym) >= 0)
801 SYMBOL_VALUE_ADDRESS (psym) += ANOFFSET (delta, SYMBOL_SECTION (psym));
ccefe4c4
TT
802 }
803}
804
95cf5869
DE
805/* Psymtab version of find_last_source_symtab. See its definition in
806 the definition of quick_symbol_functions in symfile.h. */
83827540 807
ccefe4c4 808static struct symtab *
83827540 809psym_find_last_source_symtab (struct objfile *ofp)
ccefe4c4 810{
ccefe4c4 811 struct partial_symtab *ps;
95cf5869 812 struct partial_symtab *cs_pst = NULL;
ccefe4c4 813
b11896a5 814 ALL_OBJFILE_PSYMTABS_REQUIRED (ofp, ps)
ccefe4c4
TT
815 {
816 const char *name = ps->filename;
817 int len = strlen (name);
ad3bbd48 818
ccefe4c4
TT
819 if (!(len > 2 && (strcmp (&name[len - 2], ".h") == 0
820 || strcmp (name, "<<C++-namespaces>>") == 0)))
821 cs_pst = ps;
822 }
823
824 if (cs_pst)
825 {
826 if (cs_pst->readin)
827 {
828 internal_error (__FILE__, __LINE__,
829 _("select_source_symtab: "
830 "readin pst found and no symtabs."));
831 }
832 else
43f3e411
DE
833 {
834 struct compunit_symtab *cust = psymtab_to_symtab (ofp, cs_pst);
835
836 if (cust == NULL)
837 return NULL;
838 return compunit_primary_filetab (cust);
839 }
ccefe4c4
TT
840 }
841 return NULL;
842}
843
95cf5869
DE
844/* Psymtab version of forget_cached_source_info. See its definition in
845 the definition of quick_symbol_functions in symfile.h. */
83827540 846
ccefe4c4 847static void
83827540 848psym_forget_cached_source_info (struct objfile *objfile)
ccefe4c4
TT
849{
850 struct partial_symtab *pst;
851
b11896a5 852 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, pst)
ccefe4c4
TT
853 {
854 if (pst->fullname != NULL)
855 {
856 xfree (pst->fullname);
857 pst->fullname = NULL;
858 }
859 }
860}
861
862static void
863print_partial_symbols (struct gdbarch *gdbarch,
a121b7c1 864 struct partial_symbol **p, int count, const char *what,
ccefe4c4
TT
865 struct ui_file *outfile)
866{
867 fprintf_filtered (outfile, " %s partial symbols:\n", what);
868 while (count-- > 0)
869 {
27618ce4 870 QUIT;
ccefe4c4
TT
871 fprintf_filtered (outfile, " `%s'", SYMBOL_LINKAGE_NAME (*p));
872 if (SYMBOL_DEMANGLED_NAME (*p) != NULL)
873 {
874 fprintf_filtered (outfile, " `%s'", SYMBOL_DEMANGLED_NAME (*p));
875 }
876 fputs_filtered (", ", outfile);
877 switch (SYMBOL_DOMAIN (*p))
878 {
879 case UNDEF_DOMAIN:
880 fputs_filtered ("undefined domain, ", outfile);
881 break;
882 case VAR_DOMAIN:
0df8b418 883 /* This is the usual thing -- don't print it. */
ccefe4c4
TT
884 break;
885 case STRUCT_DOMAIN:
886 fputs_filtered ("struct domain, ", outfile);
887 break;
888 case LABEL_DOMAIN:
889 fputs_filtered ("label domain, ", outfile);
890 break;
891 default:
892 fputs_filtered ("<invalid domain>, ", outfile);
893 break;
894 }
dbccfd4c 895 switch (PSYMBOL_CLASS (*p))
ccefe4c4
TT
896 {
897 case LOC_UNDEF:
898 fputs_filtered ("undefined", outfile);
899 break;
900 case LOC_CONST:
901 fputs_filtered ("constant int", outfile);
902 break;
903 case LOC_STATIC:
904 fputs_filtered ("static", outfile);
905 break;
906 case LOC_REGISTER:
907 fputs_filtered ("register", outfile);
908 break;
909 case LOC_ARG:
910 fputs_filtered ("pass by value", outfile);
911 break;
912 case LOC_REF_ARG:
913 fputs_filtered ("pass by reference", outfile);
914 break;
915 case LOC_REGPARM_ADDR:
916 fputs_filtered ("register address parameter", outfile);
917 break;
918 case LOC_LOCAL:
919 fputs_filtered ("stack parameter", outfile);
920 break;
921 case LOC_TYPEDEF:
922 fputs_filtered ("type", outfile);
923 break;
924 case LOC_LABEL:
925 fputs_filtered ("label", outfile);
926 break;
927 case LOC_BLOCK:
928 fputs_filtered ("function", outfile);
929 break;
930 case LOC_CONST_BYTES:
931 fputs_filtered ("constant bytes", outfile);
932 break;
933 case LOC_UNRESOLVED:
934 fputs_filtered ("unresolved", outfile);
935 break;
936 case LOC_OPTIMIZED_OUT:
937 fputs_filtered ("optimized out", outfile);
938 break;
939 case LOC_COMPUTED:
940 fputs_filtered ("computed at runtime", outfile);
941 break;
942 default:
943 fputs_filtered ("<invalid location>", outfile);
944 break;
945 }
946 fputs_filtered (", ", outfile);
947 fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (*p)), outfile);
948 fprintf_filtered (outfile, "\n");
949 p++;
950 }
951}
952
953static void
954dump_psymtab (struct objfile *objfile, struct partial_symtab *psymtab,
955 struct ui_file *outfile)
956{
957 struct gdbarch *gdbarch = get_objfile_arch (objfile);
958 int i;
959
b4c41fc7
DE
960 if (psymtab->anonymous)
961 {
962 fprintf_filtered (outfile, "\nAnonymous partial symtab (%s) ",
963 psymtab->filename);
964 }
965 else
966 {
967 fprintf_filtered (outfile, "\nPartial symtab for source file %s ",
968 psymtab->filename);
969 }
ccefe4c4
TT
970 fprintf_filtered (outfile, "(object ");
971 gdb_print_host_address (psymtab, outfile);
972 fprintf_filtered (outfile, ")\n\n");
973 fprintf_unfiltered (outfile, " Read from object file %s (",
4262abfb 974 objfile_name (objfile));
ccefe4c4
TT
975 gdb_print_host_address (objfile, outfile);
976 fprintf_unfiltered (outfile, ")\n");
977
978 if (psymtab->readin)
979 {
980 fprintf_filtered (outfile,
981 " Full symtab was read (at ");
43f3e411 982 gdb_print_host_address (psymtab->compunit_symtab, outfile);
ccefe4c4
TT
983 fprintf_filtered (outfile, " by function at ");
984 gdb_print_host_address (psymtab->read_symtab, outfile);
985 fprintf_filtered (outfile, ")\n");
986 }
987
ccefe4c4
TT
988 fprintf_filtered (outfile, " Symbols cover text addresses ");
989 fputs_filtered (paddress (gdbarch, psymtab->textlow), outfile);
990 fprintf_filtered (outfile, "-");
991 fputs_filtered (paddress (gdbarch, psymtab->texthigh), outfile);
992 fprintf_filtered (outfile, "\n");
9750bca9
JK
993 fprintf_filtered (outfile, " Address map supported - %s.\n",
994 psymtab->psymtabs_addrmap_supported ? "yes" : "no");
ccefe4c4
TT
995 fprintf_filtered (outfile, " Depends on %d other partial symtabs.\n",
996 psymtab->number_of_dependencies);
997 for (i = 0; i < psymtab->number_of_dependencies; i++)
998 {
999 fprintf_filtered (outfile, " %d ", i);
1000 gdb_print_host_address (psymtab->dependencies[i], outfile);
1001 fprintf_filtered (outfile, " %s\n",
1002 psymtab->dependencies[i]->filename);
1003 }
9439a077
TT
1004 if (psymtab->user != NULL)
1005 {
1006 fprintf_filtered (outfile, " Shared partial symtab with user ");
1007 gdb_print_host_address (psymtab->user, outfile);
1008 fprintf_filtered (outfile, "\n");
1009 }
ccefe4c4
TT
1010 if (psymtab->n_global_syms > 0)
1011 {
1012 print_partial_symbols (gdbarch,
af5bf4ad 1013 &objfile->global_psymbols[psymtab->globals_offset],
ccefe4c4
TT
1014 psymtab->n_global_syms, "Global", outfile);
1015 }
1016 if (psymtab->n_static_syms > 0)
1017 {
1018 print_partial_symbols (gdbarch,
af5bf4ad 1019 &objfile->static_psymbols[psymtab->statics_offset],
ccefe4c4
TT
1020 psymtab->n_static_syms, "Static", outfile);
1021 }
1022 fprintf_filtered (outfile, "\n");
1023}
1024
95cf5869
DE
1025/* Psymtab version of print_stats. See its definition in
1026 the definition of quick_symbol_functions in symfile.h. */
83827540 1027
ccefe4c4 1028static void
83827540 1029psym_print_stats (struct objfile *objfile)
ccefe4c4
TT
1030{
1031 int i;
1032 struct partial_symtab *ps;
ad3bbd48 1033
ccefe4c4 1034 i = 0;
b11896a5 1035 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
ccefe4c4
TT
1036 {
1037 if (ps->readin == 0)
1038 i++;
1039 }
1040 printf_filtered (_(" Number of psym tables (not yet expanded): %d\n"), i);
1041}
1042
95cf5869
DE
1043/* Psymtab version of dump. See its definition in
1044 the definition of quick_symbol_functions in symfile.h. */
83827540 1045
ccefe4c4 1046static void
83827540 1047psym_dump (struct objfile *objfile)
ccefe4c4
TT
1048{
1049 struct partial_symtab *psymtab;
1050
1051 if (objfile->psymtabs)
1052 {
1053 printf_filtered ("Psymtabs:\n");
1054 for (psymtab = objfile->psymtabs;
1055 psymtab != NULL;
1056 psymtab = psymtab->next)
1057 {
1058 printf_filtered ("%s at ",
1059 psymtab->filename);
1060 gdb_print_host_address (psymtab, gdb_stdout);
1061 printf_filtered (", ");
ccefe4c4
TT
1062 wrap_here (" ");
1063 }
1064 printf_filtered ("\n\n");
1065 }
1066}
1067
95cf5869
DE
1068/* Psymtab version of expand_symtabs_for_function. See its definition in
1069 the definition of quick_symbol_functions in symfile.h. */
ccefe4c4
TT
1070
1071static void
83827540
DE
1072psym_expand_symtabs_for_function (struct objfile *objfile,
1073 const char *func_name)
ccefe4c4
TT
1074{
1075 struct partial_symtab *ps;
1076
b11896a5 1077 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
ccefe4c4
TT
1078 {
1079 if (ps->readin)
1080 continue;
1081
5c80ed9d 1082 if ((lookup_partial_symbol (objfile, ps, func_name, 1, VAR_DOMAIN)
ccefe4c4 1083 != NULL)
5c80ed9d 1084 || (lookup_partial_symbol (objfile, ps, func_name, 0, VAR_DOMAIN)
ccefe4c4 1085 != NULL))
5c80ed9d 1086 psymtab_to_symtab (objfile, ps);
ccefe4c4
TT
1087 }
1088}
1089
95cf5869
DE
1090/* Psymtab version of expand_all_symtabs. See its definition in
1091 the definition of quick_symbol_functions in symfile.h. */
83827540 1092
ccefe4c4 1093static void
83827540 1094psym_expand_all_symtabs (struct objfile *objfile)
ccefe4c4
TT
1095{
1096 struct partial_symtab *psymtab;
1097
b11896a5 1098 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, psymtab)
ccefe4c4 1099 {
5c80ed9d 1100 psymtab_to_symtab (objfile, psymtab);
ccefe4c4
TT
1101 }
1102}
1103
95cf5869
DE
1104/* Psymtab version of expand_symtabs_with_fullname. See its definition in
1105 the definition of quick_symbol_functions in symfile.h. */
83827540 1106
ccefe4c4 1107static void
83827540
DE
1108psym_expand_symtabs_with_fullname (struct objfile *objfile,
1109 const char *fullname)
ccefe4c4
TT
1110{
1111 struct partial_symtab *p;
1112
b11896a5 1113 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, p)
ccefe4c4 1114 {
b4c41fc7
DE
1115 /* Anonymous psymtabs don't have a name of a source file. */
1116 if (p->anonymous)
1117 continue;
1118
5ff888ce
DE
1119 /* psymtab_to_fullname tries to open the file which is slow.
1120 Don't call it if we know the basenames don't match. */
1121 if ((basenames_may_differ
1122 || filename_cmp (lbasename (fullname), lbasename (p->filename)) == 0)
1123 && filename_cmp (fullname, psymtab_to_fullname (p)) == 0)
5c80ed9d 1124 psymtab_to_symtab (objfile, p);
ccefe4c4
TT
1125 }
1126}
1127
95cf5869
DE
1128/* Psymtab version of map_symbol_filenames. See its definition in
1129 the definition of quick_symbol_functions in symfile.h. */
83827540 1130
ccefe4c4 1131static void
83827540
DE
1132psym_map_symbol_filenames (struct objfile *objfile,
1133 symbol_filename_ftype *fun, void *data,
1134 int need_fullname)
ccefe4c4
TT
1135{
1136 struct partial_symtab *ps;
1137
b11896a5 1138 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
ccefe4c4
TT
1139 {
1140 const char *fullname;
1141
1142 if (ps->readin)
1143 continue;
1144
f80c6f3f
DE
1145 /* We can skip shared psymtabs here, because any file name will be
1146 attached to the unshared psymtab. */
1147 if (ps->user != NULL)
1148 continue;
1149
b4c41fc7
DE
1150 /* Anonymous psymtabs don't have a file name. */
1151 if (ps->anonymous)
1152 continue;
1153
821296b7 1154 QUIT;
74e2f255
DE
1155 if (need_fullname)
1156 fullname = psymtab_to_fullname (ps);
1157 else
1158 fullname = NULL;
2837d59e 1159 (*fun) (ps->filename, fullname, data);
ccefe4c4
TT
1160 }
1161}
1162
ccefe4c4
TT
1163/* Finds the fullname that a partial_symtab represents.
1164
1165 If this functions finds the fullname, it will save it in ps->fullname
1166 and it will also return the value.
1167
1168 If this function fails to find the file that this partial_symtab represents,
1169 NULL will be returned and ps->fullname will be set to NULL. */
256f06f3 1170
da5132d3 1171static const char *
ccefe4c4
TT
1172psymtab_to_fullname (struct partial_symtab *ps)
1173{
fbd9ab74 1174 gdb_assert (!ps->anonymous);
ccefe4c4 1175
256f06f3
DE
1176 /* Use cached copy if we have it.
1177 We rely on forget_cached_source_info being called appropriately
1178 to handle cases like the file being moved. */
fbd9ab74
JK
1179 if (ps->fullname == NULL)
1180 {
1181 int fd = find_and_open_source (ps->filename, ps->dirname, &ps->fullname);
256f06f3 1182
fbd9ab74
JK
1183 if (fd >= 0)
1184 close (fd);
1185 else
1186 {
0b581c69 1187 gdb::unique_xmalloc_ptr<char> fullname;
ccefe4c4 1188
fbd9ab74
JK
1189 /* rewrite_source_path would be applied by find_and_open_source, we
1190 should report the pathname where GDB tried to find the file. */
ccefe4c4 1191
fbd9ab74 1192 if (ps->dirname == NULL || IS_ABSOLUTE_PATH (ps->filename))
0b581c69 1193 fullname.reset (xstrdup (ps->filename));
fbd9ab74 1194 else
0b581c69
TT
1195 fullname.reset (concat (ps->dirname, SLASH_STRING,
1196 ps->filename, (char *) NULL));
fbd9ab74 1197
0b581c69 1198 ps->fullname = rewrite_source_path (fullname.get ()).release ();
fbd9ab74 1199 if (ps->fullname == NULL)
0b581c69 1200 ps->fullname = fullname.release ();
fbd9ab74 1201 }
95cf5869 1202 }
fbd9ab74
JK
1203
1204 return ps->fullname;
ccefe4c4
TT
1205}
1206
95cf5869
DE
1207/* For all symbols, s, in BLOCK that are in DOMAIN and match NAME
1208 according to the function MATCH, call CALLBACK(BLOCK, s, DATA).
1209 BLOCK is assumed to come from OBJFILE. Returns 1 iff CALLBACK
1210 ever returns non-zero, and otherwise returns 0. */
ccefe4c4 1211
40658b94 1212static int
fe978cb0 1213map_block (const char *name, domain_enum domain, struct objfile *objfile,
40658b94
PH
1214 struct block *block,
1215 int (*callback) (struct block *, struct symbol *, void *),
2edb89d3 1216 void *data, symbol_compare_ftype *match)
ccefe4c4 1217{
8157b174 1218 struct block_iterator iter;
40658b94 1219 struct symbol *sym;
ccefe4c4 1220
8157b174
TT
1221 for (sym = block_iter_match_first (block, name, match, &iter);
1222 sym != NULL; sym = block_iter_match_next (name, match, &iter))
ccefe4c4 1223 {
95cf5869 1224 if (symbol_matches_domain (SYMBOL_LANGUAGE (sym),
fe978cb0 1225 SYMBOL_DOMAIN (sym), domain))
40658b94
PH
1226 {
1227 if (callback (block, sym, data))
1228 return 1;
1229 }
ccefe4c4
TT
1230 }
1231
40658b94 1232 return 0;
ccefe4c4
TT
1233}
1234
95cf5869
DE
1235/* Psymtab version of map_matching_symbols. See its definition in
1236 the definition of quick_symbol_functions in symfile.h. */
40658b94 1237
ccefe4c4 1238static void
83827540 1239psym_map_matching_symbols (struct objfile *objfile,
fe978cb0 1240 const char *name, domain_enum domain,
83827540
DE
1241 int global,
1242 int (*callback) (struct block *,
1243 struct symbol *, void *),
1244 void *data,
1245 symbol_compare_ftype *match,
1246 symbol_compare_ftype *ordered_compare)
ccefe4c4 1247{
40658b94 1248 const int block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
ccefe4c4
TT
1249 struct partial_symtab *ps;
1250
b11896a5 1251 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
ccefe4c4
TT
1252 {
1253 QUIT;
1254 if (ps->readin
fe978cb0 1255 || match_partial_symbol (objfile, ps, global, name, domain, match,
40658b94 1256 ordered_compare))
ccefe4c4 1257 {
43f3e411 1258 struct compunit_symtab *cust = psymtab_to_symtab (objfile, ps);
40658b94 1259 struct block *block;
ad3bbd48 1260
43f3e411 1261 if (cust == NULL)
ccefe4c4 1262 continue;
43f3e411 1263 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
fe978cb0 1264 if (map_block (name, domain, objfile, block,
40658b94
PH
1265 callback, data, match))
1266 return;
1267 if (callback (block, NULL, data))
1268 return;
ccefe4c4
TT
1269 }
1270 }
95cf5869 1271}
ccefe4c4 1272
14bc53a8
PA
1273/* A helper for psym_expand_symtabs_matching that handles searching
1274 included psymtabs. This returns true if a symbol is found, and
1275 false otherwise. It also updates the 'searched_flag' on the
9439a077
TT
1276 various psymtabs that it searches. */
1277
14bc53a8
PA
1278static bool
1279recursively_search_psymtabs
1280 (struct partial_symtab *ps, struct objfile *objfile, enum search_domain kind,
1281 gdb::function_view<expand_symtabs_symbol_matcher_ftype> sym_matcher)
9439a077 1282{
9439a077 1283 int keep_going = 1;
f486487f 1284 enum psymtab_search_status result = PST_SEARCHED_AND_NOT_FOUND;
9439a077
TT
1285 int i;
1286
1287 if (ps->searched_flag != PST_NOT_SEARCHED)
1288 return ps->searched_flag == PST_SEARCHED_AND_FOUND;
1289
1290 /* Recurse into shared psymtabs first, because they may have already
1291 been searched, and this could save some time. */
1292 for (i = 0; i < ps->number_of_dependencies; ++i)
1293 {
1294 int r;
1295
1296 /* Skip non-shared dependencies, these are handled elsewhere. */
1297 if (ps->dependencies[i]->user == NULL)
1298 continue;
1299
1300 r = recursively_search_psymtabs (ps->dependencies[i],
14bc53a8 1301 objfile, kind, sym_matcher);
9439a077
TT
1302 if (r != 0)
1303 {
1304 ps->searched_flag = PST_SEARCHED_AND_FOUND;
14bc53a8 1305 return true;
9439a077
TT
1306 }
1307 }
1308
af5bf4ad
SM
1309 partial_symbol **gbound
1310 = &objfile->global_psymbols[ps->globals_offset + ps->n_global_syms];
1311 partial_symbol **sbound
1312 = &objfile->static_psymbols[ps->statics_offset + ps->n_static_syms];
1313 partial_symbol **bound = gbound;
9439a077
TT
1314
1315 /* Go through all of the symbols stored in a partial
1316 symtab in one loop. */
af5bf4ad 1317 partial_symbol **psym = &objfile->global_psymbols[ps->globals_offset];
9439a077
TT
1318 while (keep_going)
1319 {
1320 if (psym >= bound)
1321 {
1322 if (bound == gbound && ps->n_static_syms != 0)
1323 {
af5bf4ad 1324 psym = &objfile->static_psymbols[ps->statics_offset];
9439a077
TT
1325 bound = sbound;
1326 }
1327 else
1328 keep_going = 0;
1329 continue;
1330 }
1331 else
1332 {
1333 QUIT;
1334
1335 if ((kind == ALL_DOMAIN
1336 || (kind == VARIABLES_DOMAIN
dbccfd4c
TT
1337 && PSYMBOL_CLASS (*psym) != LOC_TYPEDEF
1338 && PSYMBOL_CLASS (*psym) != LOC_BLOCK)
9439a077 1339 || (kind == FUNCTIONS_DOMAIN
dbccfd4c 1340 && PSYMBOL_CLASS (*psym) == LOC_BLOCK)
9439a077 1341 || (kind == TYPES_DOMAIN
dbccfd4c 1342 && PSYMBOL_CLASS (*psym) == LOC_TYPEDEF))
14bc53a8 1343 && sym_matcher (SYMBOL_SEARCH_NAME (*psym)))
9439a077
TT
1344 {
1345 /* Found a match, so notify our caller. */
1346 result = PST_SEARCHED_AND_FOUND;
1347 keep_going = 0;
1348 }
1349 }
1350 psym++;
1351 }
1352
1353 ps->searched_flag = result;
1354 return result == PST_SEARCHED_AND_FOUND;
1355}
1356
95cf5869
DE
1357/* Psymtab version of expand_symtabs_matching. See its definition in
1358 the definition of quick_symbol_functions in symfile.h. */
83827540 1359
ccefe4c4 1360static void
83827540 1361psym_expand_symtabs_matching
f8eba3c6 1362 (struct objfile *objfile,
14bc53a8
PA
1363 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
1364 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
1365 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
1366 enum search_domain kind)
ccefe4c4
TT
1367{
1368 struct partial_symtab *ps;
1369
9439a077 1370 /* Clear the search flags. */
b11896a5 1371 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
ccefe4c4 1372 {
9439a077
TT
1373 ps->searched_flag = PST_NOT_SEARCHED;
1374 }
ccefe4c4 1375
9439a077
TT
1376 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
1377 {
30b3dd9d
DE
1378 QUIT;
1379
ccefe4c4
TT
1380 if (ps->readin)
1381 continue;
1382
9439a077
TT
1383 /* We skip shared psymtabs because file-matching doesn't apply
1384 to them; but we search them later in the loop. */
1385 if (ps->user != NULL)
ccefe4c4
TT
1386 continue;
1387
b4c41fc7
DE
1388 if (file_matcher)
1389 {
14bc53a8 1390 bool match;
680d1742 1391
b4c41fc7
DE
1392 if (ps->anonymous)
1393 continue;
fbd9ab74 1394
14bc53a8 1395 match = file_matcher (ps->filename, false);
680d1742
DE
1396 if (!match)
1397 {
1398 /* Before we invoke realpath, which can get expensive when many
1399 files are involved, do a quick comparison of the basenames. */
1400 if (basenames_may_differ
14bc53a8
PA
1401 || file_matcher (lbasename (ps->filename), true))
1402 match = file_matcher (psymtab_to_fullname (ps), false);
680d1742
DE
1403 }
1404 if (!match)
b4c41fc7
DE
1405 continue;
1406 }
ccefe4c4 1407
14bc53a8 1408 if (recursively_search_psymtabs (ps, objfile, kind, symbol_matcher))
276d885b
GB
1409 {
1410 struct compunit_symtab *symtab =
1411 psymtab_to_symtab (objfile, ps);
1412
1413 if (expansion_notify != NULL)
14bc53a8 1414 expansion_notify (symtab);
276d885b 1415 }
ccefe4c4
TT
1416 }
1417}
1418
95cf5869
DE
1419/* Psymtab version of has_symbols. See its definition in
1420 the definition of quick_symbol_functions in symfile.h. */
83827540 1421
ccefe4c4 1422static int
83827540 1423psym_has_symbols (struct objfile *objfile)
ccefe4c4
TT
1424{
1425 return objfile->psymtabs != NULL;
1426}
1427
1428const struct quick_symbol_functions psym_functions =
1429{
83827540
DE
1430 psym_has_symbols,
1431 psym_find_last_source_symtab,
1432 psym_forget_cached_source_info,
1433 psym_map_symtabs_matching_filename,
1434 psym_lookup_symbol,
1435 psym_print_stats,
1436 psym_dump,
1437 psym_relocate,
1438 psym_expand_symtabs_for_function,
1439 psym_expand_all_symtabs,
1440 psym_expand_symtabs_with_fullname,
1441 psym_map_matching_symbols,
1442 psym_expand_symtabs_matching,
1443 psym_find_pc_sect_compunit_symtab,
1444 psym_map_symbol_filenames
ccefe4c4
TT
1445};
1446
1447\f
1448
8763cede 1449static void
5c80ed9d 1450sort_pst_symbols (struct objfile *objfile, struct partial_symtab *pst)
ccefe4c4 1451{
0df8b418 1452 /* Sort the global list; don't sort the static list. */
af5bf4ad
SM
1453 auto begin = objfile->global_psymbols.begin ();
1454 std::advance (begin, pst->globals_offset);
1455
1456 /* The psymbols for this partial_symtab are currently at the end of the
1457 vector. */
1458 auto end = objfile->global_psymbols.end ();
ccefe4c4 1459
af5bf4ad
SM
1460 std::sort (begin, end, [] (partial_symbol *s1, partial_symbol *s2)
1461 {
1462 return strcmp_iw_ordered (SYMBOL_SEARCH_NAME (s1),
1463 SYMBOL_SEARCH_NAME (s2)) < 0;
1464 });
ccefe4c4
TT
1465}
1466
1467/* Allocate and partially fill a partial symtab. It will be
1468 completely filled at the end of the symbol list.
1469
0df8b418 1470 FILENAME is the name of the symbol-file we are reading from. */
ccefe4c4
TT
1471
1472struct partial_symtab *
1473start_psymtab_common (struct objfile *objfile,
ccefe4c4 1474 const char *filename,
af5bf4ad
SM
1475 CORE_ADDR textlow,
1476 std::vector<partial_symbol *> &global_psymbols,
1477 std::vector<partial_symbol *> &static_psymbols)
ccefe4c4
TT
1478{
1479 struct partial_symtab *psymtab;
1480
1481 psymtab = allocate_psymtab (filename, objfile);
ccefe4c4
TT
1482 psymtab->textlow = textlow;
1483 psymtab->texthigh = psymtab->textlow; /* default */
af5bf4ad
SM
1484 psymtab->globals_offset = global_psymbols.size ();
1485 psymtab->statics_offset = static_psymbols.size ();
95cf5869 1486 return psymtab;
ccefe4c4
TT
1487}
1488
8763cede
DE
1489/* Perform "finishing up" operations of a partial symtab. */
1490
1491void
1492end_psymtab_common (struct objfile *objfile, struct partial_symtab *pst)
1493{
af5bf4ad
SM
1494 pst->n_global_syms = objfile->global_psymbols.size () - pst->globals_offset;
1495 pst->n_static_syms = objfile->static_psymbols.size () - pst->statics_offset;
8763cede
DE
1496
1497 sort_pst_symbols (objfile, pst);
1498}
1499
cbd70537
SW
1500/* Calculate a hash code for the given partial symbol. The hash is
1501 calculated using the symbol's value, language, domain, class
0df8b418 1502 and name. These are the values which are set by
cbd70537
SW
1503 add_psymbol_to_bcache. */
1504
710e1a31 1505static unsigned long
cbd70537
SW
1506psymbol_hash (const void *addr, int length)
1507{
1508 unsigned long h = 0;
1509 struct partial_symbol *psymbol = (struct partial_symbol *) addr;
1510 unsigned int lang = psymbol->ginfo.language;
1511 unsigned int domain = PSYMBOL_DOMAIN (psymbol);
fe978cb0 1512 unsigned int theclass = PSYMBOL_CLASS (psymbol);
cbd70537
SW
1513
1514 h = hash_continue (&psymbol->ginfo.value, sizeof (psymbol->ginfo.value), h);
1515 h = hash_continue (&lang, sizeof (unsigned int), h);
1516 h = hash_continue (&domain, sizeof (unsigned int), h);
fe978cb0 1517 h = hash_continue (&theclass, sizeof (unsigned int), h);
cbd70537
SW
1518 h = hash_continue (psymbol->ginfo.name, strlen (psymbol->ginfo.name), h);
1519
1520 return h;
1521}
1522
1523/* Returns true if the symbol at addr1 equals the symbol at addr2.
1524 For the comparison this function uses a symbols value,
1525 language, domain, class and name. */
1526
710e1a31 1527static int
cbd70537
SW
1528psymbol_compare (const void *addr1, const void *addr2, int length)
1529{
1530 struct partial_symbol *sym1 = (struct partial_symbol *) addr1;
1531 struct partial_symbol *sym2 = (struct partial_symbol *) addr2;
1532
3e611445 1533 return (memcmp (&sym1->ginfo.value, &sym2->ginfo.value,
cbd70537
SW
1534 sizeof (sym1->ginfo.value)) == 0
1535 && sym1->ginfo.language == sym2->ginfo.language
1536 && PSYMBOL_DOMAIN (sym1) == PSYMBOL_DOMAIN (sym2)
1537 && PSYMBOL_CLASS (sym1) == PSYMBOL_CLASS (sym2)
1538 && sym1->ginfo.name == sym2->ginfo.name);
1539}
1540
710e1a31
SW
1541/* Initialize a partial symbol bcache. */
1542
1543struct psymbol_bcache *
1544psymbol_bcache_init (void)
1545{
fc270c35 1546 struct psymbol_bcache *bcache = XCNEW (struct psymbol_bcache);
95cf5869 1547
710e1a31
SW
1548 bcache->bcache = bcache_xmalloc (psymbol_hash, psymbol_compare);
1549 return bcache;
1550}
1551
1552/* Free a partial symbol bcache. */
95cf5869 1553
710e1a31
SW
1554void
1555psymbol_bcache_free (struct psymbol_bcache *bcache)
1556{
1557 if (bcache == NULL)
1558 return;
1559
1560 bcache_xfree (bcache->bcache);
1561 xfree (bcache);
1562}
1563
0df8b418 1564/* Return the internal bcache of the psymbol_bcache BCACHE. */
710e1a31
SW
1565
1566struct bcache *
1567psymbol_bcache_get_bcache (struct psymbol_bcache *bcache)
1568{
1569 return bcache->bcache;
1570}
1571
1572/* Find a copy of the SYM in BCACHE. If BCACHE has never seen this
1573 symbol before, add a copy to BCACHE. In either case, return a pointer
1574 to BCACHE's copy of the symbol. If optional ADDED is not NULL, return
1575 1 in case of new entry or 0 if returning an old entry. */
1576
af5bf4ad 1577static struct partial_symbol *
710e1a31
SW
1578psymbol_bcache_full (struct partial_symbol *sym,
1579 struct psymbol_bcache *bcache,
1580 int *added)
1581{
af5bf4ad 1582 return ((struct partial_symbol *)
19ba03f4
SM
1583 bcache_full (sym, sizeof (struct partial_symbol), bcache->bcache,
1584 added));
710e1a31
SW
1585}
1586
95cf5869 1587/* Helper function, initialises partial symbol structure and stashes
ccefe4c4
TT
1588 it into objfile's bcache. Note that our caching mechanism will
1589 use all fields of struct partial_symbol to determine hash value of the
1590 structure. In other words, having two symbols with the same name but
1591 different domain (or address) is possible and correct. */
1592
af5bf4ad 1593static struct partial_symbol *
72b9f47f 1594add_psymbol_to_bcache (const char *name, int namelength, int copy_name,
ccefe4c4 1595 domain_enum domain,
fe978cb0 1596 enum address_class theclass,
1762568f 1597 CORE_ADDR coreaddr,
ccefe4c4
TT
1598 enum language language, struct objfile *objfile,
1599 int *added)
1600{
cbd70537
SW
1601 struct partial_symbol psymbol;
1602
f85f34ed
TT
1603 /* We must ensure that the entire struct has been zeroed before
1604 assigning to it, because an assignment may not touch some of the
1605 holes. */
1606 memset (&psymbol, 0, sizeof (psymbol));
fc956729 1607
1762568f 1608 SYMBOL_VALUE_ADDRESS (&psymbol) = coreaddr;
e27d198c 1609 SYMBOL_SECTION (&psymbol) = -1;
f85f34ed 1610 SYMBOL_SET_LANGUAGE (&psymbol, language, &objfile->objfile_obstack);
ccefe4c4 1611 PSYMBOL_DOMAIN (&psymbol) = domain;
fe978cb0 1612 PSYMBOL_CLASS (&psymbol) = theclass;
ccefe4c4
TT
1613
1614 SYMBOL_SET_NAMES (&psymbol, name, namelength, copy_name, objfile);
1615
0df8b418 1616 /* Stash the partial symbol away in the cache. */
95cf5869 1617 return psymbol_bcache_full (&psymbol, objfile->psymbol_cache, added);
ccefe4c4
TT
1618}
1619
95cf5869 1620/* Helper function, adds partial symbol to the given partial symbol list. */
ccefe4c4
TT
1621
1622static void
af5bf4ad
SM
1623append_psymbol_to_list (std::vector<partial_symbol *> *list,
1624 struct partial_symbol *psym,
ccefe4c4
TT
1625 struct objfile *objfile)
1626{
af5bf4ad 1627 list->push_back (psym);
ccefe4c4
TT
1628 OBJSTAT (objfile, n_psyms++);
1629}
1630
1631/* Add a symbol with a long value to a psymtab.
1632 Since one arg is a struct, we pass in a ptr and deref it (sigh).
1762568f
DE
1633 The only value we need to store for psyms is an address.
1634 For all other psyms pass zero for COREADDR.
ccefe4c4
TT
1635 Return the partial symbol that has been added. */
1636
7dc25483 1637void
72b9f47f 1638add_psymbol_to_list (const char *name, int namelength, int copy_name,
ccefe4c4 1639 domain_enum domain,
fe978cb0 1640 enum address_class theclass,
af5bf4ad 1641 std::vector<partial_symbol *> *list,
1762568f 1642 CORE_ADDR coreaddr,
ccefe4c4
TT
1643 enum language language, struct objfile *objfile)
1644{
af5bf4ad 1645 struct partial_symbol *psym;
ccefe4c4
TT
1646
1647 int added;
1648
0df8b418 1649 /* Stash the partial symbol away in the cache. */
fe978cb0 1650 psym = add_psymbol_to_bcache (name, namelength, copy_name, domain, theclass,
1762568f 1651 coreaddr, language, objfile, &added);
ccefe4c4
TT
1652
1653 /* Do not duplicate global partial symbols. */
1654 if (list == &objfile->global_psymbols
1655 && !added)
7dc25483 1656 return;
ccefe4c4 1657
0df8b418 1658 /* Save pointer to partial symbol in psymtab, growing symtab if needed. */
ccefe4c4 1659 append_psymbol_to_list (list, psym, objfile);
ccefe4c4
TT
1660}
1661
1662/* Initialize storage for partial symbols. */
1663
1664void
1665init_psymbol_list (struct objfile *objfile, int total_symbols)
1666{
1667 /* Free any previously allocated psymbol lists. */
af5bf4ad
SM
1668 objfile->global_psymbols.clear ();
1669 objfile->static_psymbols.clear ();
ccefe4c4
TT
1670
1671 /* Current best guess is that approximately a twentieth
1672 of the total symbols (in a debugging file) are global or static
ca9c6ee2 1673 oriented symbols, then multiply that by slop factor of two. */
af5bf4ad
SM
1674 objfile->global_psymbols.reserve (total_symbols / 10);
1675 objfile->static_psymbols.reserve (total_symbols / 10);
ccefe4c4
TT
1676}
1677
1678struct partial_symtab *
1679allocate_psymtab (const char *filename, struct objfile *objfile)
1680{
1681 struct partial_symtab *psymtab;
1682
1683 if (objfile->free_psymtabs)
1684 {
1685 psymtab = objfile->free_psymtabs;
1686 objfile->free_psymtabs = psymtab->next;
1687 }
1688 else
1689 psymtab = (struct partial_symtab *)
1690 obstack_alloc (&objfile->objfile_obstack,
1691 sizeof (struct partial_symtab));
1692
1693 memset (psymtab, 0, sizeof (struct partial_symtab));
19ba03f4
SM
1694 psymtab->filename
1695 = (const char *) bcache (filename, strlen (filename) + 1,
1696 objfile->per_bfd->filename_cache);
43f3e411 1697 psymtab->compunit_symtab = NULL;
ccefe4c4
TT
1698
1699 /* Prepend it to the psymtab list for the objfile it belongs to.
1700 Psymtabs are searched in most recent inserted -> least recent
0df8b418 1701 inserted order. */
ccefe4c4 1702
ccefe4c4
TT
1703 psymtab->next = objfile->psymtabs;
1704 objfile->psymtabs = psymtab;
ccefe4c4 1705
45cfd468
DE
1706 if (symtab_create_debug)
1707 {
1708 /* Be a bit clever with debugging messages, and don't print objfile
1709 every time, only when it changes. */
1710 static char *last_objfile_name = NULL;
1711
1712 if (last_objfile_name == NULL
4262abfb 1713 || strcmp (last_objfile_name, objfile_name (objfile)) != 0)
45cfd468
DE
1714 {
1715 xfree (last_objfile_name);
4262abfb 1716 last_objfile_name = xstrdup (objfile_name (objfile));
45cfd468
DE
1717 fprintf_unfiltered (gdb_stdlog,
1718 "Creating one or more psymtabs for objfile %s ...\n",
1719 last_objfile_name);
1720 }
1721 fprintf_unfiltered (gdb_stdlog,
fd55216c
DE
1722 "Created psymtab %s for module %s.\n",
1723 host_address_to_string (psymtab), filename);
45cfd468
DE
1724 }
1725
95cf5869 1726 return psymtab;
ccefe4c4
TT
1727}
1728
1729void
5c80ed9d 1730discard_psymtab (struct objfile *objfile, struct partial_symtab *pst)
ccefe4c4
TT
1731{
1732 struct partial_symtab **prev_pst;
1733
1734 /* From dbxread.c:
1735 Empty psymtabs happen as a result of header files which don't
1736 have any symbols in them. There can be a lot of them. But this
1737 check is wrong, in that a psymtab with N_SLINE entries but
1738 nothing else is not empty, but we don't realize that. Fixing
1739 that without slowing things down might be tricky. */
1740
0df8b418 1741 /* First, snip it out of the psymtab chain. */
ccefe4c4 1742
5c80ed9d 1743 prev_pst = &(objfile->psymtabs);
ccefe4c4
TT
1744 while ((*prev_pst) != pst)
1745 prev_pst = &((*prev_pst)->next);
1746 (*prev_pst) = pst->next;
1747
0df8b418 1748 /* Next, put it on a free list for recycling. */
ccefe4c4 1749
5c80ed9d
TT
1750 pst->next = objfile->free_psymtabs;
1751 objfile->free_psymtabs = pst;
ccefe4c4
TT
1752}
1753
ccefe4c4
TT
1754\f
1755
372405a5
DE
1756/* We need to pass a couple of items to the addrmap_foreach function,
1757 so use a struct. */
1758
1759struct dump_psymtab_addrmap_data
1760{
1761 struct objfile *objfile;
1762 struct partial_symtab *psymtab;
1763 struct ui_file *outfile;
1764
1765 /* Non-zero if the previously printed addrmap entry was for PSYMTAB.
1766 If so, we want to print the next one as well (since the next addrmap
1767 entry defines the end of the range). */
1768 int previous_matched;
1769};
1770
1771/* Helper function for dump_psymtab_addrmap to print an addrmap entry. */
1772
1773static int
1774dump_psymtab_addrmap_1 (void *datap, CORE_ADDR start_addr, void *obj)
1775{
709b5518
PA
1776 struct dump_psymtab_addrmap_data *data
1777 = (struct dump_psymtab_addrmap_data *) datap;
372405a5 1778 struct gdbarch *gdbarch = get_objfile_arch (data->objfile);
709b5518 1779 struct partial_symtab *addrmap_psymtab = (struct partial_symtab *) obj;
372405a5
DE
1780 const char *psymtab_address_or_end = NULL;
1781
1782 QUIT;
1783
1784 if (data->psymtab == NULL
1785 || data->psymtab == addrmap_psymtab)
1786 psymtab_address_or_end = host_address_to_string (addrmap_psymtab);
1787 else if (data->previous_matched)
1788 psymtab_address_or_end = "<ends here>";
1789
1790 if (data->psymtab == NULL
1791 || data->psymtab == addrmap_psymtab
1792 || data->previous_matched)
1793 {
1794 fprintf_filtered (data->outfile, " %s%s %s\n",
1795 data->psymtab != NULL ? " " : "",
1796 paddress (gdbarch, start_addr),
1797 psymtab_address_or_end);
1798 }
1799
1800 data->previous_matched = (data->psymtab == NULL
1801 || data->psymtab == addrmap_psymtab);
1802
1803 return 0;
1804}
1805
1806/* Helper function for maintenance_print_psymbols to print the addrmap
1807 of PSYMTAB. If PSYMTAB is NULL print the entire addrmap. */
1808
1809static void
1810dump_psymtab_addrmap (struct objfile *objfile, struct partial_symtab *psymtab,
1811 struct ui_file *outfile)
1812{
1813 struct dump_psymtab_addrmap_data addrmap_dump_data;
1814
34c41c68
DE
1815 if ((psymtab == NULL
1816 || psymtab->psymtabs_addrmap_supported)
1817 && objfile->psymtabs_addrmap != NULL)
372405a5
DE
1818 {
1819 addrmap_dump_data.objfile = objfile;
1820 addrmap_dump_data.psymtab = psymtab;
1821 addrmap_dump_data.outfile = outfile;
1822 addrmap_dump_data.previous_matched = 0;
1823 fprintf_filtered (outfile, "%sddress map:\n",
1824 psymtab == NULL ? "Entire a" : " A");
1825 addrmap_foreach (objfile->psymtabs_addrmap, dump_psymtab_addrmap_1,
1826 &addrmap_dump_data);
1827 }
1828}
1829
dfc7bb5b 1830static void
990b9f9f 1831maintenance_print_psymbols (const char *args, int from_tty)
ccefe4c4 1832{
34c41c68 1833 struct ui_file *outfile = gdb_stdout;
34c41c68 1834 char *address_arg = NULL, *source_arg = NULL, *objfile_arg = NULL;
ccefe4c4
TT
1835 struct objfile *objfile;
1836 struct partial_symtab *ps;
34c41c68
DE
1837 int i, outfile_idx, found;
1838 CORE_ADDR pc = 0;
1839 struct obj_section *section = NULL;
ccefe4c4
TT
1840
1841 dont_repeat ();
1842
773a1edc 1843 gdb_argv argv (args);
ccefe4c4 1844
99e8a4f9 1845 for (i = 0; argv != NULL && argv[i] != NULL; ++i)
ccefe4c4 1846 {
34c41c68
DE
1847 if (strcmp (argv[i], "-pc") == 0)
1848 {
1849 if (argv[i + 1] == NULL)
1850 error (_("Missing pc value"));
1851 address_arg = argv[++i];
1852 }
1853 else if (strcmp (argv[i], "-source") == 0)
1854 {
1855 if (argv[i + 1] == NULL)
1856 error (_("Missing source file"));
1857 source_arg = argv[++i];
1858 }
1859 else if (strcmp (argv[i], "-objfile") == 0)
1860 {
1861 if (argv[i + 1] == NULL)
1862 error (_("Missing objfile name"));
1863 objfile_arg = argv[++i];
1864 }
1865 else if (strcmp (argv[i], "--") == 0)
1866 {
1867 /* End of options. */
1868 ++i;
1869 break;
1870 }
1871 else if (argv[i][0] == '-')
ccefe4c4 1872 {
34c41c68
DE
1873 /* Future proofing: Don't allow OUTFILE to begin with "-". */
1874 error (_("Unknown option: %s"), argv[i]);
ccefe4c4 1875 }
34c41c68
DE
1876 else
1877 break;
ccefe4c4 1878 }
34c41c68 1879 outfile_idx = i;
ccefe4c4 1880
34c41c68
DE
1881 if (address_arg != NULL && source_arg != NULL)
1882 error (_("Must specify at most one of -pc and -source"));
ccefe4c4 1883
d7e74731
PA
1884 stdio_file arg_outfile;
1885
99e8a4f9 1886 if (argv != NULL && argv[outfile_idx] != NULL)
34c41c68 1887 {
34c41c68
DE
1888 if (argv[outfile_idx + 1] != NULL)
1889 error (_("Junk at end of command"));
ee0c3293
TT
1890 gdb::unique_xmalloc_ptr<char> outfile_name
1891 (tilde_expand (argv[outfile_idx]));
1892 if (!arg_outfile.open (outfile_name.get (), FOPEN_WT))
1893 perror_with_name (outfile_name.get ());
d7e74731 1894 outfile = &arg_outfile;
34c41c68 1895 }
ccefe4c4 1896
34c41c68
DE
1897 if (address_arg != NULL)
1898 {
1899 pc = parse_and_eval_address (address_arg);
1900 /* If we fail to find a section, that's ok, try the lookup anyway. */
1901 section = find_pc_section (pc);
1902 }
372405a5 1903
34c41c68
DE
1904 found = 0;
1905 ALL_OBJFILES (objfile)
27618ce4 1906 {
34c41c68
DE
1907 int printed_objfile_header = 0;
1908 int print_for_objfile = 1;
1909
27618ce4 1910 QUIT;
34c41c68
DE
1911 if (objfile_arg != NULL)
1912 print_for_objfile
1913 = compare_filenames_for_search (objfile_name (objfile),
1914 objfile_arg);
1915 if (!print_for_objfile)
1916 continue;
1917
1918 if (address_arg != NULL)
1919 {
1920 struct bound_minimal_symbol msymbol = { NULL, NULL };
1921
1922 /* We don't assume each pc has a unique objfile (this is for
1923 debugging). */
1924 ps = find_pc_sect_psymtab (objfile, pc, section, msymbol);
1925 if (ps != NULL)
1926 {
1927 if (!printed_objfile_header)
1928 {
d7e74731
PA
1929 outfile->printf ("\nPartial symtabs for objfile %s\n",
1930 objfile_name (objfile));
34c41c68
DE
1931 printed_objfile_header = 1;
1932 }
1933 dump_psymtab (objfile, ps, outfile);
1934 dump_psymtab_addrmap (objfile, ps, outfile);
1935 found = 1;
1936 }
1937 }
1938 else
1939 {
1940 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
1941 {
1942 int print_for_source = 0;
1943
1944 QUIT;
1945 if (source_arg != NULL)
1946 {
1947 print_for_source
1948 = compare_filenames_for_search (ps->filename, source_arg);
1949 found = 1;
1950 }
1951 if (source_arg == NULL
1952 || print_for_source)
1953 {
1954 if (!printed_objfile_header)
1955 {
d7e74731
PA
1956 outfile->printf ("\nPartial symtabs for objfile %s\n",
1957 objfile_name (objfile));
34c41c68
DE
1958 printed_objfile_header = 1;
1959 }
1960 dump_psymtab (objfile, ps, outfile);
1961 dump_psymtab_addrmap (objfile, ps, outfile);
1962 }
1963 }
1964 }
1965
1966 /* If we're printing all the objfile's symbols dump the full addrmap. */
1967
1968 if (address_arg == NULL
1969 && source_arg == NULL
1970 && objfile->psymtabs_addrmap != NULL)
372405a5 1971 {
d7e74731 1972 outfile->puts ("\n");
34c41c68 1973 dump_psymtab_addrmap (objfile, NULL, outfile);
372405a5 1974 }
27618ce4 1975 }
372405a5 1976
34c41c68
DE
1977 if (!found)
1978 {
1979 if (address_arg != NULL)
1980 error (_("No partial symtab for address: %s"), address_arg);
1981 if (source_arg != NULL)
1982 error (_("No partial symtab for source file: %s"), source_arg);
1983 }
ccefe4c4
TT
1984}
1985
1986/* List all the partial symbol tables whose names match REGEXP (optional). */
95cf5869 1987
dfc7bb5b 1988static void
990b9f9f 1989maintenance_info_psymtabs (const char *regexp, int from_tty)
ccefe4c4
TT
1990{
1991 struct program_space *pspace;
1992 struct objfile *objfile;
1993
1994 if (regexp)
1995 re_comp (regexp);
1996
1997 ALL_PSPACES (pspace)
1998 ALL_PSPACE_OBJFILES (pspace, objfile)
1999 {
2000 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2001 struct partial_symtab *psymtab;
2002
2003 /* We don't want to print anything for this objfile until we
2004 actually find a symtab whose name matches. */
2005 int printed_objfile_start = 0;
2006
b11896a5 2007 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, psymtab)
ccefe4c4
TT
2008 {
2009 QUIT;
2010
2011 if (! regexp
2012 || re_exec (psymtab->filename))
2013 {
2014 if (! printed_objfile_start)
2015 {
4262abfb 2016 printf_filtered ("{ objfile %s ", objfile_name (objfile));
ccefe4c4 2017 wrap_here (" ");
95cf5869 2018 printf_filtered ("((struct objfile *) %s)\n",
ccefe4c4
TT
2019 host_address_to_string (objfile));
2020 printed_objfile_start = 1;
2021 }
2022
2023 printf_filtered (" { psymtab %s ", psymtab->filename);
2024 wrap_here (" ");
95cf5869 2025 printf_filtered ("((struct partial_symtab *) %s)\n",
ccefe4c4
TT
2026 host_address_to_string (psymtab));
2027
2028 printf_filtered (" readin %s\n",
2029 psymtab->readin ? "yes" : "no");
2030 printf_filtered (" fullname %s\n",
3e43a32a
MS
2031 psymtab->fullname
2032 ? psymtab->fullname : "(null)");
ccefe4c4
TT
2033 printf_filtered (" text addresses ");
2034 fputs_filtered (paddress (gdbarch, psymtab->textlow),
2035 gdb_stdout);
2036 printf_filtered (" -- ");
2037 fputs_filtered (paddress (gdbarch, psymtab->texthigh),
2038 gdb_stdout);
2039 printf_filtered ("\n");
9750bca9
JK
2040 printf_filtered (" psymtabs_addrmap_supported %s\n",
2041 (psymtab->psymtabs_addrmap_supported
2042 ? "yes" : "no"));
ccefe4c4
TT
2043 printf_filtered (" globals ");
2044 if (psymtab->n_global_syms)
2045 {
af5bf4ad
SM
2046 auto p = &objfile->global_psymbols[psymtab->globals_offset];
2047
ccefe4c4 2048 printf_filtered ("(* (struct partial_symbol **) %s @ %d)\n",
af5bf4ad 2049 host_address_to_string (p),
ccefe4c4
TT
2050 psymtab->n_global_syms);
2051 }
2052 else
2053 printf_filtered ("(none)\n");
2054 printf_filtered (" statics ");
2055 if (psymtab->n_static_syms)
2056 {
af5bf4ad
SM
2057 auto p = &objfile->static_psymbols[psymtab->statics_offset];
2058
ccefe4c4 2059 printf_filtered ("(* (struct partial_symbol **) %s @ %d)\n",
af5bf4ad 2060 host_address_to_string (p),
ccefe4c4
TT
2061 psymtab->n_static_syms);
2062 }
2063 else
2064 printf_filtered ("(none)\n");
2065 printf_filtered (" dependencies ");
2066 if (psymtab->number_of_dependencies)
2067 {
2068 int i;
2069
2070 printf_filtered ("{\n");
2071 for (i = 0; i < psymtab->number_of_dependencies; i++)
2072 {
2073 struct partial_symtab *dep = psymtab->dependencies[i];
2074
2075 /* Note the string concatenation there --- no comma. */
2076 printf_filtered (" psymtab %s "
2077 "((struct partial_symtab *) %s)\n",
95cf5869 2078 dep->filename,
ccefe4c4
TT
2079 host_address_to_string (dep));
2080 }
2081 printf_filtered (" }\n");
2082 }
2083 else
2084 printf_filtered ("(none)\n");
2085 printf_filtered (" }\n");
2086 }
2087 }
2088
2089 if (printed_objfile_start)
2090 printf_filtered ("}\n");
2091 }
2092}
2093
7d0c9981 2094/* Check consistency of currently expanded psymtabs vs symtabs. */
ccefe4c4 2095
dfc7bb5b 2096static void
990b9f9f 2097maintenance_check_psymtabs (const char *ignore, int from_tty)
ccefe4c4
TT
2098{
2099 struct symbol *sym;
43f3e411 2100 struct compunit_symtab *cust = NULL;
ccefe4c4 2101 struct partial_symtab *ps;
346d1dfe 2102 const struct blockvector *bv;
ccefe4c4
TT
2103 struct objfile *objfile;
2104 struct block *b;
2105 int length;
2106
2107 ALL_PSYMTABS (objfile, ps)
2108 {
2109 struct gdbarch *gdbarch = get_objfile_arch (objfile);
ad3bbd48 2110
7d0c9981
DE
2111 /* We don't call psymtab_to_symtab here because that may cause symtab
2112 expansion. When debugging a problem it helps if checkers leave
2113 things unchanged. */
43f3e411 2114 cust = ps->compunit_symtab;
7d0c9981
DE
2115
2116 /* First do some checks that don't require the associated symtab. */
2117 if (ps->texthigh < ps->textlow)
2118 {
2119 printf_filtered ("Psymtab ");
2120 puts_filtered (ps->filename);
2121 printf_filtered (" covers bad range ");
2122 fputs_filtered (paddress (gdbarch, ps->textlow), gdb_stdout);
2123 printf_filtered (" - ");
2124 fputs_filtered (paddress (gdbarch, ps->texthigh), gdb_stdout);
2125 printf_filtered ("\n");
2126 continue;
2127 }
2128
2129 /* Now do checks requiring the associated symtab. */
43f3e411 2130 if (cust == NULL)
ccefe4c4 2131 continue;
43f3e411 2132 bv = COMPUNIT_BLOCKVECTOR (cust);
ccefe4c4 2133 b = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
af5bf4ad 2134 partial_symbol **psym = &objfile->static_psymbols[ps->statics_offset];
ccefe4c4
TT
2135 length = ps->n_static_syms;
2136 while (length--)
2137 {
16b2eaa1 2138 sym = block_lookup_symbol (b, SYMBOL_LINKAGE_NAME (*psym),
ccefe4c4
TT
2139 SYMBOL_DOMAIN (*psym));
2140 if (!sym)
2141 {
2142 printf_filtered ("Static symbol `");
2143 puts_filtered (SYMBOL_LINKAGE_NAME (*psym));
2144 printf_filtered ("' only found in ");
2145 puts_filtered (ps->filename);
2146 printf_filtered (" psymtab\n");
2147 }
2148 psym++;
2149 }
2150 b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
af5bf4ad 2151 psym = &objfile->global_psymbols[ps->globals_offset];
ccefe4c4
TT
2152 length = ps->n_global_syms;
2153 while (length--)
2154 {
16b2eaa1 2155 sym = block_lookup_symbol (b, SYMBOL_LINKAGE_NAME (*psym),
ccefe4c4
TT
2156 SYMBOL_DOMAIN (*psym));
2157 if (!sym)
2158 {
2159 printf_filtered ("Global symbol `");
2160 puts_filtered (SYMBOL_LINKAGE_NAME (*psym));
2161 printf_filtered ("' only found in ");
2162 puts_filtered (ps->filename);
2163 printf_filtered (" psymtab\n");
2164 }
2165 psym++;
2166 }
7d0c9981
DE
2167 if (ps->texthigh != 0
2168 && (ps->textlow < BLOCK_START (b) || ps->texthigh > BLOCK_END (b)))
ccefe4c4
TT
2169 {
2170 printf_filtered ("Psymtab ");
2171 puts_filtered (ps->filename);
2172 printf_filtered (" covers ");
2173 fputs_filtered (paddress (gdbarch, ps->textlow), gdb_stdout);
2174 printf_filtered (" - ");
2175 fputs_filtered (paddress (gdbarch, ps->texthigh), gdb_stdout);
2176 printf_filtered (" but symtab covers only ");
2177 fputs_filtered (paddress (gdbarch, BLOCK_START (b)), gdb_stdout);
2178 printf_filtered (" - ");
2179 fputs_filtered (paddress (gdbarch, BLOCK_END (b)), gdb_stdout);
2180 printf_filtered ("\n");
2181 }
2182 }
2183}
2184
dfc7bb5b
YQ
2185void
2186_initialize_psymtab (void)
2187{
2188 add_cmd ("psymbols", class_maintenance, maintenance_print_psymbols, _("\
2189Print dump of current partial symbol definitions.\n\
34c41c68
DE
2190Usage: mt print psymbols [-objfile objfile] [-pc address] [--] [outfile]\n\
2191 mt print psymbols [-objfile objfile] [-source source] [--] [outfile]\n\
2192Entries in the partial symbol table are dumped to file OUTFILE,\n\
2193or the terminal if OUTFILE is unspecified.\n\
2194If ADDRESS is provided, dump only the file for that address.\n\
2195If SOURCE is provided, dump only that file's symbols.\n\
2196If OBJFILE is provided, dump only that file's minimal symbols."),
dfc7bb5b
YQ
2197 &maintenanceprintlist);
2198
2199 add_cmd ("psymtabs", class_maintenance, maintenance_info_psymtabs, _("\
2200List the partial symbol tables for all object files.\n\
2201This does not include information about individual partial symbols,\n\
2202just the symbol table structures themselves."),
2203 &maintenanceinfolist);
2204
7d0c9981
DE
2205 add_cmd ("check-psymtabs", class_maintenance, maintenance_check_psymtabs,
2206 _("\
2207Check consistency of currently expanded psymtabs versus symtabs."),
dfc7bb5b
YQ
2208 &maintenancelist);
2209}