]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/psymtab.c
Introduce partial_symbol::address
[thirdparty/binutils-gdb.git] / gdb / psymtab.c
CommitLineData
ccefe4c4 1/* Partial symbol tables.
95cf5869 2
e2882c85 3 Copyright (C) 2009-2018 Free Software Foundation, Inc.
ccefe4c4
TT
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20#include "defs.h"
21#include "symtab.h"
22#include "psympriv.h"
23#include "objfiles.h"
ccefe4c4
TT
24#include "block.h"
25#include "filenames.h"
26#include "source.h"
27#include "addrmap.h"
28#include "gdbtypes.h"
29#include "bcache.h"
30#include "ui-out.h"
31#include "command.h"
32#include "readline/readline.h"
33#include "gdb_regex.h"
40658b94 34#include "dictionary.h"
c00f8484
KS
35#include "language.h"
36#include "cp-support.h"
dfc7bb5b 37#include "gdbcmd.h"
af5bf4ad 38#include <algorithm>
71a3c369 39#include <set>
ccefe4c4 40
710e1a31
SW
41struct psymbol_bcache
42{
43 struct bcache *bcache;
44};
45
5c80ed9d
TT
46static struct partial_symbol *match_partial_symbol (struct objfile *,
47 struct partial_symtab *,
40658b94
PH
48 int,
49 const char *, domain_enum,
b5ec771e 50 symbol_name_match_type,
2edb89d3 51 symbol_compare_ftype *);
40658b94 52
5c80ed9d
TT
53static struct partial_symbol *lookup_partial_symbol (struct objfile *,
54 struct partial_symtab *,
ccefe4c4
TT
55 const char *, int,
56 domain_enum);
57
da5132d3 58static const char *psymtab_to_fullname (struct partial_symtab *ps);
ccefe4c4 59
5c80ed9d
TT
60static struct partial_symbol *find_pc_sect_psymbol (struct objfile *,
61 struct partial_symtab *,
ccefe4c4
TT
62 CORE_ADDR,
63 struct obj_section *);
64
50da2f25
YQ
65static void fixup_psymbol_section (struct partial_symbol *psym,
66 struct objfile *objfile);
ccefe4c4 67
43f3e411
DE
68static struct compunit_symtab *psymtab_to_symtab (struct objfile *objfile,
69 struct partial_symtab *pst);
ccefe4c4 70
b11896a5
TT
71/* Ensure that the partial symbols for OBJFILE have been loaded. This
72 function always returns its argument, as a convenience. */
73
74struct objfile *
75require_partial_symbols (struct objfile *objfile, int verbose)
76{
77 if ((objfile->flags & OBJF_PSYMTABS_READ) == 0)
78 {
79 objfile->flags |= OBJF_PSYMTABS_READ;
80
81 if (objfile->sf->sym_read_psymbols)
82 {
83 if (verbose)
84 {
85 printf_unfiltered (_("Reading symbols from %s..."),
4262abfb 86 objfile_name (objfile));
b11896a5
TT
87 gdb_flush (gdb_stdout);
88 }
89 (*objfile->sf->sym_read_psymbols) (objfile);
af5bf4ad
SM
90
91 /* Partial symbols list are not expected to changed after this
92 point. */
93 objfile->global_psymbols.shrink_to_fit ();
94 objfile->static_psymbols.shrink_to_fit ();
95
b11896a5
TT
96 if (verbose)
97 {
98 if (!objfile_has_symbols (objfile))
99 {
100 wrap_here ("");
101 printf_unfiltered (_("(no debugging symbols found)..."));
102 wrap_here ("");
103 }
104
105 printf_unfiltered (_("done.\n"));
106 }
107 }
108 }
109
110 return objfile;
111}
112
113/* Traverse all psymtabs in one objfile, requiring that the psymtabs
114 be read in. */
115
116#define ALL_OBJFILE_PSYMTABS_REQUIRED(objfile, p) \
117 for ((p) = require_partial_symbols (objfile, 1)->psymtabs; \
118 (p) != NULL; \
119 (p) = (p)->next)
120
121/* We want to make sure this file always requires psymtabs. */
122
123#undef ALL_OBJFILE_PSYMTABS
124
125/* Traverse all psymtabs in all objfiles. */
126
127#define ALL_PSYMTABS(objfile, p) \
128 ALL_OBJFILES (objfile) \
129 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, p)
130
83827540 131/* Helper function for psym_map_symtabs_matching_filename that
f8eba3c6 132 expands the symtabs and calls the iterator. */
ccefe4c4 133
14bc53a8 134static bool
f8eba3c6
TT
135partial_map_expand_apply (struct objfile *objfile,
136 const char *name,
f8eba3c6
TT
137 const char *real_path,
138 struct partial_symtab *pst,
14bc53a8 139 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 140{
43f3e411 141 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6 142
9439a077
TT
143 /* Shared psymtabs should never be seen here. Instead they should
144 be handled properly by the caller. */
145 gdb_assert (pst->user == NULL);
146
f8eba3c6
TT
147 /* Don't visit already-expanded psymtabs. */
148 if (pst->readin)
149 return 0;
150
151 /* This may expand more than one symtab, and we want to iterate over
152 all of them. */
5c80ed9d 153 psymtab_to_symtab (objfile, pst);
f8eba3c6 154
14bc53a8
PA
155 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
156 last_made, callback);
f8eba3c6
TT
157}
158
83827540
DE
159/* Psymtab version of map_symtabs_matching_filename. See its definition in
160 the definition of quick_symbol_functions in symfile.h. */
f8eba3c6 161
14bc53a8
PA
162static bool
163psym_map_symtabs_matching_filename
164 (struct objfile *objfile,
165 const char *name,
166 const char *real_path,
167 gdb::function_view<bool (symtab *)> callback)
ccefe4c4
TT
168{
169 struct partial_symtab *pst;
c011a4f4 170 const char *name_basename = lbasename (name);
ccefe4c4 171
b11896a5 172 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, pst)
ccefe4c4 173 {
9439a077
TT
174 /* We can skip shared psymtabs here, because any file name will be
175 attached to the unshared psymtab. */
176 if (pst->user != NULL)
177 continue;
178
b4c41fc7
DE
179 /* Anonymous psymtabs don't have a file name. */
180 if (pst->anonymous)
181 continue;
182
af529f8f 183 if (compare_filenames_for_search (pst->filename, name))
ccefe4c4 184 {
f5b95b50 185 if (partial_map_expand_apply (objfile, name, real_path,
14bc53a8
PA
186 pst, callback))
187 return true;
288e77a7 188 continue;
ccefe4c4
TT
189 }
190
c011a4f4
DE
191 /* Before we invoke realpath, which can get expensive when many
192 files are involved, do a quick comparison of the basenames. */
193 if (! basenames_may_differ
194 && FILENAME_CMP (name_basename, lbasename (pst->filename)) != 0)
195 continue;
196
05cba821
JK
197 if (compare_filenames_for_search (psymtab_to_fullname (pst), name))
198 {
199 if (partial_map_expand_apply (objfile, name, real_path,
14bc53a8
PA
200 pst, callback))
201 return true;
288e77a7 202 continue;
05cba821
JK
203 }
204
ccefe4c4
TT
205 /* If the user gave us an absolute path, try to find the file in
206 this symtab and use its absolute path. */
ccefe4c4
TT
207 if (real_path != NULL)
208 {
af529f8f
JK
209 gdb_assert (IS_ABSOLUTE_PATH (real_path));
210 gdb_assert (IS_ABSOLUTE_PATH (name));
fbd9ab74 211 if (filename_cmp (psymtab_to_fullname (pst), real_path) == 0)
ccefe4c4 212 {
f5b95b50 213 if (partial_map_expand_apply (objfile, name, real_path,
14bc53a8
PA
214 pst, callback))
215 return true;
288e77a7 216 continue;
ccefe4c4
TT
217 }
218 }
219 }
220
14bc53a8 221 return false;
ccefe4c4
TT
222}
223
224/* Find which partial symtab contains PC and SECTION starting at psymtab PST.
225 We may find a different psymtab than PST. See FIND_PC_SECT_PSYMTAB. */
226
227static struct partial_symtab *
5c80ed9d
TT
228find_pc_sect_psymtab_closer (struct objfile *objfile,
229 CORE_ADDR pc, struct obj_section *section,
ccefe4c4 230 struct partial_symtab *pst,
77e371c0 231 struct bound_minimal_symbol msymbol)
ccefe4c4 232{
ccefe4c4
TT
233 struct partial_symtab *tpst;
234 struct partial_symtab *best_pst = pst;
235 CORE_ADDR best_addr = pst->textlow;
236
9750bca9
JK
237 gdb_assert (!pst->psymtabs_addrmap_supported);
238
ccefe4c4
TT
239 /* An objfile that has its functions reordered might have
240 many partial symbol tables containing the PC, but
241 we want the partial symbol table that contains the
242 function containing the PC. */
95cf5869
DE
243 if (!(objfile->flags & OBJF_REORDERED)
244 && section == NULL) /* Can't validate section this way. */
ccefe4c4
TT
245 return pst;
246
77e371c0 247 if (msymbol.minsym == NULL)
95cf5869 248 return pst;
ccefe4c4
TT
249
250 /* The code range of partial symtabs sometimes overlap, so, in
251 the loop below, we need to check all partial symtabs and
0df8b418 252 find the one that fits better for the given PC address. We
ccefe4c4
TT
253 select the partial symtab that contains a symbol whose
254 address is closest to the PC address. By closest we mean
255 that find_pc_sect_symbol returns the symbol with address
256 that is closest and still less than the given PC. */
257 for (tpst = pst; tpst != NULL; tpst = tpst->next)
258 {
259 if (pc >= tpst->textlow && pc < tpst->texthigh)
260 {
261 struct partial_symbol *p;
262 CORE_ADDR this_addr;
263
264 /* NOTE: This assumes that every psymbol has a
265 corresponding msymbol, which is not necessarily
266 true; the debug info might be much richer than the
267 object's symbol table. */
5c80ed9d 268 p = find_pc_sect_psymbol (objfile, tpst, pc, section);
ccefe4c4 269 if (p != NULL
02e9e7f7 270 && (p->address (objfile) == 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)
02e9e7f7 278 this_addr = p->address (objfile);
ccefe4c4
TT
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 335 if (p == NULL
02e9e7f7
TT
336 || (p->address (objfile)
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
8a6d4234
TT
427 if (p->domain == VAR_DOMAIN
428 && p->aclass == LOC_BLOCK
02e9e7f7
TT
429 && pc >= p->address (objfile)
430 && (p->address (objfile) > best_pc
ccefe4c4 431 || (psymtab->textlow == 0
02e9e7f7 432 && best_pc == 0 && p->address (objfile) == 0)))
ccefe4c4 433 {
95cf5869 434 if (section != NULL) /* Match on a specific section. */
ccefe4c4 435 {
5c80ed9d 436 fixup_psymbol_section (p, objfile);
8a6d4234 437 if (!matching_obj_sections (p->obj_section (objfile),
e27d198c 438 section))
ccefe4c4
TT
439 continue;
440 }
02e9e7f7 441 best_pc = p->address (objfile);
ccefe4c4
TT
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
8a6d4234
TT
450 if (p->domain == VAR_DOMAIN
451 && p->aclass == LOC_BLOCK
02e9e7f7
TT
452 && pc >= p->address (objfile)
453 && (p->address (objfile) > best_pc
ccefe4c4 454 || (psymtab->textlow == 0
02e9e7f7 455 && best_pc == 0 && p->address (objfile) == 0)))
ccefe4c4 456 {
95cf5869 457 if (section != NULL) /* Match on a specific section. */
ccefe4c4 458 {
5c80ed9d 459 fixup_psymbol_section (p, objfile);
8a6d4234 460 if (!matching_obj_sections (p->obj_section (objfile),
e27d198c 461 section))
ccefe4c4
TT
462 continue;
463 }
02e9e7f7 464 best_pc = p->address (objfile);
ccefe4c4
TT
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
8a6d4234 480 if (psym->section >= 0)
50da2f25 481 return;
ccefe4c4
TT
482
483 gdb_assert (objfile);
484
8a6d4234 485 switch (psym->aclass)
ccefe4c4
TT
486 {
487 case LOC_STATIC:
488 case LOC_LABEL:
489 case LOC_BLOCK:
02e9e7f7 490 addr = psym->address (objfile);
ccefe4c4
TT
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
8a6d4234 498 fixup_section (psym, 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{
8a6d4234 556 const language_defn *lang = language_def (psym->language);
b5ec771e 557 symbol_name_matcher_ftype *name_match
618daa93 558 = get_symbol_name_matcher (lang, lookup_name);
8a6d4234 559 return name_match (symbol_search_name (psym), lookup_name, NULL);
b5ec771e
PA
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 608
8a6d4234 609 enum language lang = (*center)->language;
b5ec771e
PA
610 const char *lang_ln
611 = lookup_name.language_lookup_name (lang).c_str ();
612
8a6d4234 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 {
8a6d4234
TT
623 if (symbol_matches_domain ((*top)->language,
624 (*top)->domain, 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 {
8a6d4234
TT
637 if (symbol_matches_domain ((*psym)->language,
638 (*psym)->domain, 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"));
8a6d4234 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. */
8a6d4234 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
8a6d4234 742 while (top <= real_top && symbol_matches_search_name (*top, lookup_name))
ccefe4c4 743 {
8a6d4234
TT
744 if (symbol_matches_domain ((*top)->language,
745 (*top)->domain, 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 {
8a6d4234
TT
758 if (symbol_matches_domain ((*psym)->language,
759 (*psym)->domain, domain)
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 804{
ccefe4c4
TT
805 struct partial_symtab *p;
806
b11896a5 807 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, p)
ccefe4c4
TT
808 {
809 p->textlow += ANOFFSET (delta, SECT_OFF_TEXT (objfile));
810 p->texthigh += ANOFFSET (delta, SECT_OFF_TEXT (objfile));
811 }
812
af5bf4ad 813 for (partial_symbol *psym : objfile->global_psymbols)
ccefe4c4 814 {
af5bf4ad 815 fixup_psymbol_section (psym, objfile);
8a6d4234 816 if (psym->section >= 0)
02e9e7f7
TT
817 psym->set_address (psym->unrelocated_address ()
818 + ANOFFSET (delta, psym->section));
ccefe4c4 819 }
af5bf4ad 820 for (partial_symbol *psym : objfile->static_psymbols)
ccefe4c4 821 {
af5bf4ad 822 fixup_psymbol_section (psym, objfile);
8a6d4234 823 if (psym->section >= 0)
02e9e7f7
TT
824 psym->set_address (psym->unrelocated_address ()
825 + ANOFFSET (delta, psym->section));
ccefe4c4 826 }
71a3c369
TT
827
828 objfile->psymbol_map.clear ();
ccefe4c4
TT
829}
830
95cf5869
DE
831/* Psymtab version of find_last_source_symtab. See its definition in
832 the definition of quick_symbol_functions in symfile.h. */
83827540 833
ccefe4c4 834static struct symtab *
83827540 835psym_find_last_source_symtab (struct objfile *ofp)
ccefe4c4 836{
ccefe4c4 837 struct partial_symtab *ps;
95cf5869 838 struct partial_symtab *cs_pst = NULL;
ccefe4c4 839
b11896a5 840 ALL_OBJFILE_PSYMTABS_REQUIRED (ofp, ps)
ccefe4c4
TT
841 {
842 const char *name = ps->filename;
843 int len = strlen (name);
ad3bbd48 844
ccefe4c4
TT
845 if (!(len > 2 && (strcmp (&name[len - 2], ".h") == 0
846 || strcmp (name, "<<C++-namespaces>>") == 0)))
847 cs_pst = ps;
848 }
849
850 if (cs_pst)
851 {
852 if (cs_pst->readin)
853 {
854 internal_error (__FILE__, __LINE__,
855 _("select_source_symtab: "
856 "readin pst found and no symtabs."));
857 }
858 else
43f3e411
DE
859 {
860 struct compunit_symtab *cust = psymtab_to_symtab (ofp, cs_pst);
861
862 if (cust == NULL)
863 return NULL;
864 return compunit_primary_filetab (cust);
865 }
ccefe4c4
TT
866 }
867 return NULL;
868}
869
95cf5869
DE
870/* Psymtab version of forget_cached_source_info. See its definition in
871 the definition of quick_symbol_functions in symfile.h. */
83827540 872
ccefe4c4 873static void
83827540 874psym_forget_cached_source_info (struct objfile *objfile)
ccefe4c4
TT
875{
876 struct partial_symtab *pst;
877
b11896a5 878 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, pst)
ccefe4c4
TT
879 {
880 if (pst->fullname != NULL)
881 {
882 xfree (pst->fullname);
883 pst->fullname = NULL;
884 }
885 }
886}
887
888static void
02e9e7f7 889print_partial_symbols (struct gdbarch *gdbarch, struct objfile *objfile,
a121b7c1 890 struct partial_symbol **p, int count, const char *what,
ccefe4c4
TT
891 struct ui_file *outfile)
892{
893 fprintf_filtered (outfile, " %s partial symbols:\n", what);
894 while (count-- > 0)
895 {
27618ce4 896 QUIT;
8a6d4234
TT
897 fprintf_filtered (outfile, " `%s'", (*p)->name);
898 if (symbol_demangled_name (*p) != NULL)
ccefe4c4 899 {
8a6d4234 900 fprintf_filtered (outfile, " `%s'", symbol_demangled_name (*p));
ccefe4c4
TT
901 }
902 fputs_filtered (", ", outfile);
8a6d4234 903 switch ((*p)->domain)
ccefe4c4
TT
904 {
905 case UNDEF_DOMAIN:
906 fputs_filtered ("undefined domain, ", outfile);
907 break;
908 case VAR_DOMAIN:
0df8b418 909 /* This is the usual thing -- don't print it. */
ccefe4c4
TT
910 break;
911 case STRUCT_DOMAIN:
912 fputs_filtered ("struct domain, ", outfile);
913 break;
914 case LABEL_DOMAIN:
915 fputs_filtered ("label domain, ", outfile);
916 break;
917 default:
918 fputs_filtered ("<invalid domain>, ", outfile);
919 break;
920 }
8a6d4234 921 switch ((*p)->aclass)
ccefe4c4
TT
922 {
923 case LOC_UNDEF:
924 fputs_filtered ("undefined", outfile);
925 break;
926 case LOC_CONST:
927 fputs_filtered ("constant int", outfile);
928 break;
929 case LOC_STATIC:
930 fputs_filtered ("static", outfile);
931 break;
932 case LOC_REGISTER:
933 fputs_filtered ("register", outfile);
934 break;
935 case LOC_ARG:
936 fputs_filtered ("pass by value", outfile);
937 break;
938 case LOC_REF_ARG:
939 fputs_filtered ("pass by reference", outfile);
940 break;
941 case LOC_REGPARM_ADDR:
942 fputs_filtered ("register address parameter", outfile);
943 break;
944 case LOC_LOCAL:
945 fputs_filtered ("stack parameter", outfile);
946 break;
947 case LOC_TYPEDEF:
948 fputs_filtered ("type", outfile);
949 break;
950 case LOC_LABEL:
951 fputs_filtered ("label", outfile);
952 break;
953 case LOC_BLOCK:
954 fputs_filtered ("function", outfile);
955 break;
956 case LOC_CONST_BYTES:
957 fputs_filtered ("constant bytes", outfile);
958 break;
959 case LOC_UNRESOLVED:
960 fputs_filtered ("unresolved", outfile);
961 break;
962 case LOC_OPTIMIZED_OUT:
963 fputs_filtered ("optimized out", outfile);
964 break;
965 case LOC_COMPUTED:
966 fputs_filtered ("computed at runtime", outfile);
967 break;
968 default:
969 fputs_filtered ("<invalid location>", outfile);
970 break;
971 }
972 fputs_filtered (", ", outfile);
02e9e7f7 973 fputs_filtered (paddress (gdbarch, (*p)->unrelocated_address ()), outfile);
ccefe4c4
TT
974 fprintf_filtered (outfile, "\n");
975 p++;
976 }
977}
978
979static void
980dump_psymtab (struct objfile *objfile, struct partial_symtab *psymtab,
981 struct ui_file *outfile)
982{
983 struct gdbarch *gdbarch = get_objfile_arch (objfile);
984 int i;
985
b4c41fc7
DE
986 if (psymtab->anonymous)
987 {
988 fprintf_filtered (outfile, "\nAnonymous partial symtab (%s) ",
989 psymtab->filename);
990 }
991 else
992 {
993 fprintf_filtered (outfile, "\nPartial symtab for source file %s ",
994 psymtab->filename);
995 }
ccefe4c4
TT
996 fprintf_filtered (outfile, "(object ");
997 gdb_print_host_address (psymtab, outfile);
998 fprintf_filtered (outfile, ")\n\n");
999 fprintf_unfiltered (outfile, " Read from object file %s (",
4262abfb 1000 objfile_name (objfile));
ccefe4c4
TT
1001 gdb_print_host_address (objfile, outfile);
1002 fprintf_unfiltered (outfile, ")\n");
1003
1004 if (psymtab->readin)
1005 {
1006 fprintf_filtered (outfile,
1007 " Full symtab was read (at ");
43f3e411 1008 gdb_print_host_address (psymtab->compunit_symtab, outfile);
ccefe4c4
TT
1009 fprintf_filtered (outfile, " by function at ");
1010 gdb_print_host_address (psymtab->read_symtab, outfile);
1011 fprintf_filtered (outfile, ")\n");
1012 }
1013
ccefe4c4
TT
1014 fprintf_filtered (outfile, " Symbols cover text addresses ");
1015 fputs_filtered (paddress (gdbarch, psymtab->textlow), outfile);
1016 fprintf_filtered (outfile, "-");
1017 fputs_filtered (paddress (gdbarch, psymtab->texthigh), outfile);
1018 fprintf_filtered (outfile, "\n");
9750bca9
JK
1019 fprintf_filtered (outfile, " Address map supported - %s.\n",
1020 psymtab->psymtabs_addrmap_supported ? "yes" : "no");
ccefe4c4
TT
1021 fprintf_filtered (outfile, " Depends on %d other partial symtabs.\n",
1022 psymtab->number_of_dependencies);
1023 for (i = 0; i < psymtab->number_of_dependencies; i++)
1024 {
1025 fprintf_filtered (outfile, " %d ", i);
1026 gdb_print_host_address (psymtab->dependencies[i], outfile);
1027 fprintf_filtered (outfile, " %s\n",
1028 psymtab->dependencies[i]->filename);
1029 }
9439a077
TT
1030 if (psymtab->user != NULL)
1031 {
1032 fprintf_filtered (outfile, " Shared partial symtab with user ");
1033 gdb_print_host_address (psymtab->user, outfile);
1034 fprintf_filtered (outfile, "\n");
1035 }
ccefe4c4
TT
1036 if (psymtab->n_global_syms > 0)
1037 {
02e9e7f7 1038 print_partial_symbols (gdbarch, objfile,
af5bf4ad 1039 &objfile->global_psymbols[psymtab->globals_offset],
ccefe4c4
TT
1040 psymtab->n_global_syms, "Global", outfile);
1041 }
1042 if (psymtab->n_static_syms > 0)
1043 {
02e9e7f7 1044 print_partial_symbols (gdbarch, objfile,
af5bf4ad 1045 &objfile->static_psymbols[psymtab->statics_offset],
ccefe4c4
TT
1046 psymtab->n_static_syms, "Static", outfile);
1047 }
1048 fprintf_filtered (outfile, "\n");
1049}
1050
95cf5869
DE
1051/* Psymtab version of print_stats. See its definition in
1052 the definition of quick_symbol_functions in symfile.h. */
83827540 1053
ccefe4c4 1054static void
83827540 1055psym_print_stats (struct objfile *objfile)
ccefe4c4
TT
1056{
1057 int i;
1058 struct partial_symtab *ps;
ad3bbd48 1059
ccefe4c4 1060 i = 0;
b11896a5 1061 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
ccefe4c4
TT
1062 {
1063 if (ps->readin == 0)
1064 i++;
1065 }
1066 printf_filtered (_(" Number of psym tables (not yet expanded): %d\n"), i);
1067}
1068
95cf5869
DE
1069/* Psymtab version of dump. See its definition in
1070 the definition of quick_symbol_functions in symfile.h. */
83827540 1071
ccefe4c4 1072static void
83827540 1073psym_dump (struct objfile *objfile)
ccefe4c4
TT
1074{
1075 struct partial_symtab *psymtab;
1076
1077 if (objfile->psymtabs)
1078 {
1079 printf_filtered ("Psymtabs:\n");
1080 for (psymtab = objfile->psymtabs;
1081 psymtab != NULL;
1082 psymtab = psymtab->next)
1083 {
1084 printf_filtered ("%s at ",
1085 psymtab->filename);
1086 gdb_print_host_address (psymtab, gdb_stdout);
1087 printf_filtered (", ");
ccefe4c4
TT
1088 wrap_here (" ");
1089 }
1090 printf_filtered ("\n\n");
1091 }
1092}
1093
95cf5869
DE
1094/* Psymtab version of expand_symtabs_for_function. See its definition in
1095 the definition of quick_symbol_functions in symfile.h. */
ccefe4c4
TT
1096
1097static void
83827540
DE
1098psym_expand_symtabs_for_function (struct objfile *objfile,
1099 const char *func_name)
ccefe4c4
TT
1100{
1101 struct partial_symtab *ps;
1102
b11896a5 1103 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
ccefe4c4
TT
1104 {
1105 if (ps->readin)
1106 continue;
1107
5c80ed9d 1108 if ((lookup_partial_symbol (objfile, ps, func_name, 1, VAR_DOMAIN)
ccefe4c4 1109 != NULL)
5c80ed9d 1110 || (lookup_partial_symbol (objfile, ps, func_name, 0, VAR_DOMAIN)
ccefe4c4 1111 != NULL))
5c80ed9d 1112 psymtab_to_symtab (objfile, ps);
ccefe4c4
TT
1113 }
1114}
1115
95cf5869
DE
1116/* Psymtab version of expand_all_symtabs. See its definition in
1117 the definition of quick_symbol_functions in symfile.h. */
83827540 1118
ccefe4c4 1119static void
83827540 1120psym_expand_all_symtabs (struct objfile *objfile)
ccefe4c4
TT
1121{
1122 struct partial_symtab *psymtab;
1123
b11896a5 1124 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, psymtab)
ccefe4c4 1125 {
5c80ed9d 1126 psymtab_to_symtab (objfile, psymtab);
ccefe4c4
TT
1127 }
1128}
1129
95cf5869
DE
1130/* Psymtab version of expand_symtabs_with_fullname. See its definition in
1131 the definition of quick_symbol_functions in symfile.h. */
83827540 1132
ccefe4c4 1133static void
83827540
DE
1134psym_expand_symtabs_with_fullname (struct objfile *objfile,
1135 const char *fullname)
ccefe4c4
TT
1136{
1137 struct partial_symtab *p;
1138
b11896a5 1139 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, p)
ccefe4c4 1140 {
b4c41fc7
DE
1141 /* Anonymous psymtabs don't have a name of a source file. */
1142 if (p->anonymous)
1143 continue;
1144
5ff888ce
DE
1145 /* psymtab_to_fullname tries to open the file which is slow.
1146 Don't call it if we know the basenames don't match. */
1147 if ((basenames_may_differ
1148 || filename_cmp (lbasename (fullname), lbasename (p->filename)) == 0)
1149 && filename_cmp (fullname, psymtab_to_fullname (p)) == 0)
5c80ed9d 1150 psymtab_to_symtab (objfile, p);
ccefe4c4
TT
1151 }
1152}
1153
95cf5869
DE
1154/* Psymtab version of map_symbol_filenames. See its definition in
1155 the definition of quick_symbol_functions in symfile.h. */
83827540 1156
ccefe4c4 1157static void
83827540
DE
1158psym_map_symbol_filenames (struct objfile *objfile,
1159 symbol_filename_ftype *fun, void *data,
1160 int need_fullname)
ccefe4c4
TT
1161{
1162 struct partial_symtab *ps;
1163
b11896a5 1164 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
ccefe4c4
TT
1165 {
1166 const char *fullname;
1167
1168 if (ps->readin)
1169 continue;
1170
f80c6f3f
DE
1171 /* We can skip shared psymtabs here, because any file name will be
1172 attached to the unshared psymtab. */
1173 if (ps->user != NULL)
1174 continue;
1175
b4c41fc7
DE
1176 /* Anonymous psymtabs don't have a file name. */
1177 if (ps->anonymous)
1178 continue;
1179
821296b7 1180 QUIT;
74e2f255
DE
1181 if (need_fullname)
1182 fullname = psymtab_to_fullname (ps);
1183 else
1184 fullname = NULL;
2837d59e 1185 (*fun) (ps->filename, fullname, data);
ccefe4c4
TT
1186 }
1187}
1188
ccefe4c4
TT
1189/* Finds the fullname that a partial_symtab represents.
1190
1191 If this functions finds the fullname, it will save it in ps->fullname
1192 and it will also return the value.
1193
1194 If this function fails to find the file that this partial_symtab represents,
1195 NULL will be returned and ps->fullname will be set to NULL. */
256f06f3 1196
da5132d3 1197static const char *
ccefe4c4
TT
1198psymtab_to_fullname (struct partial_symtab *ps)
1199{
fbd9ab74 1200 gdb_assert (!ps->anonymous);
ccefe4c4 1201
256f06f3
DE
1202 /* Use cached copy if we have it.
1203 We rely on forget_cached_source_info being called appropriately
1204 to handle cases like the file being moved. */
fbd9ab74
JK
1205 if (ps->fullname == NULL)
1206 {
e0cc99a6
TT
1207 gdb::unique_xmalloc_ptr<char> fullname;
1208 int fd = find_and_open_source (ps->filename, ps->dirname, &fullname);
1209 ps->fullname = fullname.release ();
256f06f3 1210
fbd9ab74
JK
1211 if (fd >= 0)
1212 close (fd);
1213 else
1214 {
fbd9ab74
JK
1215 /* rewrite_source_path would be applied by find_and_open_source, we
1216 should report the pathname where GDB tried to find the file. */
ccefe4c4 1217
fbd9ab74 1218 if (ps->dirname == NULL || IS_ABSOLUTE_PATH (ps->filename))
0b581c69 1219 fullname.reset (xstrdup (ps->filename));
fbd9ab74 1220 else
0b581c69
TT
1221 fullname.reset (concat (ps->dirname, SLASH_STRING,
1222 ps->filename, (char *) NULL));
fbd9ab74 1223
0b581c69 1224 ps->fullname = rewrite_source_path (fullname.get ()).release ();
fbd9ab74 1225 if (ps->fullname == NULL)
0b581c69 1226 ps->fullname = fullname.release ();
fbd9ab74 1227 }
95cf5869 1228 }
fbd9ab74
JK
1229
1230 return ps->fullname;
ccefe4c4
TT
1231}
1232
95cf5869
DE
1233/* For all symbols, s, in BLOCK that are in DOMAIN and match NAME
1234 according to the function MATCH, call CALLBACK(BLOCK, s, DATA).
1235 BLOCK is assumed to come from OBJFILE. Returns 1 iff CALLBACK
1236 ever returns non-zero, and otherwise returns 0. */
ccefe4c4 1237
40658b94 1238static int
fe978cb0 1239map_block (const char *name, domain_enum domain, struct objfile *objfile,
40658b94
PH
1240 struct block *block,
1241 int (*callback) (struct block *, struct symbol *, void *),
b5ec771e 1242 void *data, symbol_name_match_type match)
ccefe4c4 1243{
8157b174 1244 struct block_iterator iter;
40658b94 1245 struct symbol *sym;
ccefe4c4 1246
b5ec771e
PA
1247 lookup_name_info lookup_name (name, match);
1248
1249 for (sym = block_iter_match_first (block, lookup_name, &iter);
1250 sym != NULL;
1251 sym = block_iter_match_next (lookup_name, &iter))
ccefe4c4 1252 {
95cf5869 1253 if (symbol_matches_domain (SYMBOL_LANGUAGE (sym),
fe978cb0 1254 SYMBOL_DOMAIN (sym), domain))
40658b94
PH
1255 {
1256 if (callback (block, sym, data))
1257 return 1;
1258 }
ccefe4c4
TT
1259 }
1260
40658b94 1261 return 0;
ccefe4c4
TT
1262}
1263
95cf5869
DE
1264/* Psymtab version of map_matching_symbols. See its definition in
1265 the definition of quick_symbol_functions in symfile.h. */
40658b94 1266
ccefe4c4 1267static void
83827540 1268psym_map_matching_symbols (struct objfile *objfile,
fe978cb0 1269 const char *name, domain_enum domain,
83827540
DE
1270 int global,
1271 int (*callback) (struct block *,
1272 struct symbol *, void *),
1273 void *data,
b5ec771e 1274 symbol_name_match_type match,
83827540 1275 symbol_compare_ftype *ordered_compare)
ccefe4c4 1276{
40658b94 1277 const int block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
ccefe4c4
TT
1278 struct partial_symtab *ps;
1279
b11896a5 1280 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
ccefe4c4
TT
1281 {
1282 QUIT;
1283 if (ps->readin
fe978cb0 1284 || match_partial_symbol (objfile, ps, global, name, domain, match,
40658b94 1285 ordered_compare))
ccefe4c4 1286 {
43f3e411 1287 struct compunit_symtab *cust = psymtab_to_symtab (objfile, ps);
40658b94 1288 struct block *block;
ad3bbd48 1289
43f3e411 1290 if (cust == NULL)
ccefe4c4 1291 continue;
43f3e411 1292 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
fe978cb0 1293 if (map_block (name, domain, objfile, block,
40658b94
PH
1294 callback, data, match))
1295 return;
1296 if (callback (block, NULL, data))
1297 return;
ccefe4c4
TT
1298 }
1299 }
95cf5869 1300}
ccefe4c4 1301
14bc53a8
PA
1302/* A helper for psym_expand_symtabs_matching that handles searching
1303 included psymtabs. This returns true if a symbol is found, and
1304 false otherwise. It also updates the 'searched_flag' on the
9439a077
TT
1305 various psymtabs that it searches. */
1306
14bc53a8
PA
1307static bool
1308recursively_search_psymtabs
b5ec771e
PA
1309 (struct partial_symtab *ps, struct objfile *objfile, enum search_domain domain,
1310 const lookup_name_info &lookup_name,
14bc53a8 1311 gdb::function_view<expand_symtabs_symbol_matcher_ftype> sym_matcher)
9439a077 1312{
9439a077 1313 int keep_going = 1;
f486487f 1314 enum psymtab_search_status result = PST_SEARCHED_AND_NOT_FOUND;
9439a077
TT
1315 int i;
1316
1317 if (ps->searched_flag != PST_NOT_SEARCHED)
1318 return ps->searched_flag == PST_SEARCHED_AND_FOUND;
1319
1320 /* Recurse into shared psymtabs first, because they may have already
1321 been searched, and this could save some time. */
1322 for (i = 0; i < ps->number_of_dependencies; ++i)
1323 {
1324 int r;
1325
1326 /* Skip non-shared dependencies, these are handled elsewhere. */
1327 if (ps->dependencies[i]->user == NULL)
1328 continue;
1329
1330 r = recursively_search_psymtabs (ps->dependencies[i],
b5ec771e
PA
1331 objfile, domain, lookup_name,
1332 sym_matcher);
9439a077
TT
1333 if (r != 0)
1334 {
1335 ps->searched_flag = PST_SEARCHED_AND_FOUND;
14bc53a8 1336 return true;
9439a077
TT
1337 }
1338 }
1339
af5bf4ad 1340 partial_symbol **gbound
47fea877 1341 = objfile->global_psymbols.data () + ps->globals_offset + ps->n_global_syms;
af5bf4ad 1342 partial_symbol **sbound
47fea877 1343 = objfile->static_psymbols.data () + ps->statics_offset + ps->n_static_syms;
af5bf4ad 1344 partial_symbol **bound = gbound;
9439a077
TT
1345
1346 /* Go through all of the symbols stored in a partial
1347 symtab in one loop. */
47fea877 1348 partial_symbol **psym = objfile->global_psymbols.data () + ps->globals_offset;
9439a077
TT
1349 while (keep_going)
1350 {
1351 if (psym >= bound)
1352 {
1353 if (bound == gbound && ps->n_static_syms != 0)
1354 {
47fea877 1355 psym = objfile->static_psymbols.data () + ps->statics_offset;
9439a077
TT
1356 bound = sbound;
1357 }
1358 else
1359 keep_going = 0;
1360 continue;
1361 }
1362 else
1363 {
1364 QUIT;
1365
b5ec771e
PA
1366 if ((domain == ALL_DOMAIN
1367 || (domain == VARIABLES_DOMAIN
8a6d4234
TT
1368 && (*psym)->aclass != LOC_TYPEDEF
1369 && (*psym)->aclass != LOC_BLOCK)
b5ec771e 1370 || (domain == FUNCTIONS_DOMAIN
8a6d4234 1371 && (*psym)->aclass == LOC_BLOCK)
b5ec771e 1372 || (domain == TYPES_DOMAIN
8a6d4234 1373 && (*psym)->aclass == LOC_TYPEDEF))
b5ec771e 1374 && psymbol_name_matches (*psym, lookup_name)
8a6d4234 1375 && (sym_matcher == NULL || sym_matcher (symbol_search_name (*psym))))
9439a077
TT
1376 {
1377 /* Found a match, so notify our caller. */
1378 result = PST_SEARCHED_AND_FOUND;
1379 keep_going = 0;
1380 }
1381 }
1382 psym++;
1383 }
1384
1385 ps->searched_flag = result;
1386 return result == PST_SEARCHED_AND_FOUND;
1387}
1388
95cf5869
DE
1389/* Psymtab version of expand_symtabs_matching. See its definition in
1390 the definition of quick_symbol_functions in symfile.h. */
83827540 1391
ccefe4c4 1392static void
83827540 1393psym_expand_symtabs_matching
f8eba3c6 1394 (struct objfile *objfile,
14bc53a8 1395 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c62446b1 1396 const lookup_name_info &lookup_name_in,
14bc53a8
PA
1397 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
1398 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
b5ec771e 1399 enum search_domain domain)
ccefe4c4
TT
1400{
1401 struct partial_symtab *ps;
1402
c62446b1
PA
1403 lookup_name_info lookup_name = lookup_name_in.make_ignore_params ();
1404
9439a077 1405 /* Clear the search flags. */
b11896a5 1406 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
ccefe4c4 1407 {
9439a077
TT
1408 ps->searched_flag = PST_NOT_SEARCHED;
1409 }
ccefe4c4 1410
9439a077
TT
1411 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
1412 {
30b3dd9d
DE
1413 QUIT;
1414
ccefe4c4
TT
1415 if (ps->readin)
1416 continue;
1417
9439a077
TT
1418 /* We skip shared psymtabs because file-matching doesn't apply
1419 to them; but we search them later in the loop. */
1420 if (ps->user != NULL)
ccefe4c4
TT
1421 continue;
1422
b4c41fc7
DE
1423 if (file_matcher)
1424 {
14bc53a8 1425 bool match;
680d1742 1426
b4c41fc7
DE
1427 if (ps->anonymous)
1428 continue;
fbd9ab74 1429
14bc53a8 1430 match = file_matcher (ps->filename, false);
680d1742
DE
1431 if (!match)
1432 {
1433 /* Before we invoke realpath, which can get expensive when many
1434 files are involved, do a quick comparison of the basenames. */
1435 if (basenames_may_differ
14bc53a8
PA
1436 || file_matcher (lbasename (ps->filename), true))
1437 match = file_matcher (psymtab_to_fullname (ps), false);
680d1742
DE
1438 }
1439 if (!match)
b4c41fc7
DE
1440 continue;
1441 }
ccefe4c4 1442
b5ec771e
PA
1443 if (recursively_search_psymtabs (ps, objfile, domain,
1444 lookup_name, symbol_matcher))
276d885b
GB
1445 {
1446 struct compunit_symtab *symtab =
1447 psymtab_to_symtab (objfile, ps);
1448
1449 if (expansion_notify != NULL)
14bc53a8 1450 expansion_notify (symtab);
276d885b 1451 }
ccefe4c4
TT
1452 }
1453}
1454
95cf5869
DE
1455/* Psymtab version of has_symbols. See its definition in
1456 the definition of quick_symbol_functions in symfile.h. */
83827540 1457
ccefe4c4 1458static int
83827540 1459psym_has_symbols (struct objfile *objfile)
ccefe4c4
TT
1460{
1461 return objfile->psymtabs != NULL;
1462}
1463
71a3c369
TT
1464/* Helper function for psym_find_compunit_symtab_by_address that fills
1465 in psymbol_map for a given range of psymbols. */
1466
1467static void
1468psym_fill_psymbol_map (struct objfile *objfile,
1469 struct partial_symtab *psymtab,
1470 std::set<CORE_ADDR> *seen_addrs,
1471 const std::vector<partial_symbol *> &symbols,
1472 int start,
1473 int length)
1474{
1475 for (int i = 0; i < length; ++i)
1476 {
1477 struct partial_symbol *psym = symbols[start + i];
1478
8a6d4234 1479 if (psym->aclass == LOC_STATIC)
71a3c369 1480 {
02e9e7f7 1481 CORE_ADDR addr = psym->address (objfile);
71a3c369
TT
1482 if (seen_addrs->find (addr) == seen_addrs->end ())
1483 {
1484 seen_addrs->insert (addr);
1485 objfile->psymbol_map.emplace_back (addr, psymtab);
1486 }
1487 }
1488 }
1489}
1490
1491/* See find_compunit_symtab_by_address in quick_symbol_functions, in
1492 symfile.h. */
1493
1494static compunit_symtab *
1495psym_find_compunit_symtab_by_address (struct objfile *objfile,
1496 CORE_ADDR address)
1497{
1498 if (objfile->psymbol_map.empty ())
1499 {
1500 struct partial_symtab *pst;
1501
1502 std::set<CORE_ADDR> seen_addrs;
1503
1504 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, pst)
1505 {
1506 psym_fill_psymbol_map (objfile, pst,
1507 &seen_addrs,
1508 objfile->global_psymbols,
1509 pst->globals_offset,
1510 pst->n_global_syms);
1511 psym_fill_psymbol_map (objfile, pst,
1512 &seen_addrs,
1513 objfile->static_psymbols,
1514 pst->statics_offset,
1515 pst->n_static_syms);
1516 }
1517
1518 objfile->psymbol_map.shrink_to_fit ();
1519
1520 std::sort (objfile->psymbol_map.begin (), objfile->psymbol_map.end (),
1521 [] (const std::pair<CORE_ADDR, partial_symtab *> &a,
1522 const std::pair<CORE_ADDR, partial_symtab *> &b)
1523 {
1524 return a.first < b.first;
1525 });
1526 }
1527
1528 auto iter = std::lower_bound
1529 (objfile->psymbol_map.begin (), objfile->psymbol_map.end (), address,
1530 [] (const std::pair<CORE_ADDR, partial_symtab *> &a,
1531 CORE_ADDR b)
1532 {
1533 return a.first < b;
1534 });
1535
1536 if (iter == objfile->psymbol_map.end () || iter->first != address)
1537 return NULL;
1538
1539 return psymtab_to_symtab (objfile, iter->second);
1540}
1541
ccefe4c4
TT
1542const struct quick_symbol_functions psym_functions =
1543{
83827540
DE
1544 psym_has_symbols,
1545 psym_find_last_source_symtab,
1546 psym_forget_cached_source_info,
1547 psym_map_symtabs_matching_filename,
1548 psym_lookup_symbol,
1549 psym_print_stats,
1550 psym_dump,
1551 psym_relocate,
1552 psym_expand_symtabs_for_function,
1553 psym_expand_all_symtabs,
1554 psym_expand_symtabs_with_fullname,
1555 psym_map_matching_symbols,
1556 psym_expand_symtabs_matching,
1557 psym_find_pc_sect_compunit_symtab,
71a3c369 1558 psym_find_compunit_symtab_by_address,
83827540 1559 psym_map_symbol_filenames
ccefe4c4
TT
1560};
1561
1562\f
1563
8763cede 1564static void
5c80ed9d 1565sort_pst_symbols (struct objfile *objfile, struct partial_symtab *pst)
ccefe4c4 1566{
0df8b418 1567 /* Sort the global list; don't sort the static list. */
af5bf4ad
SM
1568 auto begin = objfile->global_psymbols.begin ();
1569 std::advance (begin, pst->globals_offset);
1570
1571 /* The psymbols for this partial_symtab are currently at the end of the
1572 vector. */
1573 auto end = objfile->global_psymbols.end ();
ccefe4c4 1574
af5bf4ad
SM
1575 std::sort (begin, end, [] (partial_symbol *s1, partial_symbol *s2)
1576 {
8a6d4234
TT
1577 return strcmp_iw_ordered (symbol_search_name (s1),
1578 symbol_search_name (s2)) < 0;
af5bf4ad 1579 });
ccefe4c4
TT
1580}
1581
1582/* Allocate and partially fill a partial symtab. It will be
1583 completely filled at the end of the symbol list.
1584
0df8b418 1585 FILENAME is the name of the symbol-file we are reading from. */
ccefe4c4
TT
1586
1587struct partial_symtab *
1588start_psymtab_common (struct objfile *objfile,
ccefe4c4 1589 const char *filename,
af5bf4ad
SM
1590 CORE_ADDR textlow,
1591 std::vector<partial_symbol *> &global_psymbols,
1592 std::vector<partial_symbol *> &static_psymbols)
ccefe4c4
TT
1593{
1594 struct partial_symtab *psymtab;
1595
1596 psymtab = allocate_psymtab (filename, objfile);
ccefe4c4
TT
1597 psymtab->textlow = textlow;
1598 psymtab->texthigh = psymtab->textlow; /* default */
af5bf4ad
SM
1599 psymtab->globals_offset = global_psymbols.size ();
1600 psymtab->statics_offset = static_psymbols.size ();
95cf5869 1601 return psymtab;
ccefe4c4
TT
1602}
1603
8763cede
DE
1604/* Perform "finishing up" operations of a partial symtab. */
1605
1606void
1607end_psymtab_common (struct objfile *objfile, struct partial_symtab *pst)
1608{
af5bf4ad
SM
1609 pst->n_global_syms = objfile->global_psymbols.size () - pst->globals_offset;
1610 pst->n_static_syms = objfile->static_psymbols.size () - pst->statics_offset;
8763cede
DE
1611
1612 sort_pst_symbols (objfile, pst);
1613}
1614
cbd70537
SW
1615/* Calculate a hash code for the given partial symbol. The hash is
1616 calculated using the symbol's value, language, domain, class
0df8b418 1617 and name. These are the values which are set by
cbd70537
SW
1618 add_psymbol_to_bcache. */
1619
710e1a31 1620static unsigned long
cbd70537
SW
1621psymbol_hash (const void *addr, int length)
1622{
1623 unsigned long h = 0;
1624 struct partial_symbol *psymbol = (struct partial_symbol *) addr;
8a6d4234
TT
1625 unsigned int lang = psymbol->language;
1626 unsigned int domain = psymbol->domain;
1627 unsigned int theclass = psymbol->aclass;
cbd70537 1628
8a6d4234 1629 h = hash_continue (&psymbol->value, sizeof (psymbol->value), h);
cbd70537
SW
1630 h = hash_continue (&lang, sizeof (unsigned int), h);
1631 h = hash_continue (&domain, sizeof (unsigned int), h);
fe978cb0 1632 h = hash_continue (&theclass, sizeof (unsigned int), h);
e793c052
TT
1633 /* Note that psymbol names are interned via symbol_set_names, so
1634 there's no need to hash the contents of the name here. */
8a6d4234
TT
1635 h = hash_continue (&psymbol->name,
1636 sizeof (psymbol->name), h);
cbd70537
SW
1637
1638 return h;
1639}
1640
1641/* Returns true if the symbol at addr1 equals the symbol at addr2.
1642 For the comparison this function uses a symbols value,
1643 language, domain, class and name. */
1644
710e1a31 1645static int
cbd70537
SW
1646psymbol_compare (const void *addr1, const void *addr2, int length)
1647{
1648 struct partial_symbol *sym1 = (struct partial_symbol *) addr1;
1649 struct partial_symbol *sym2 = (struct partial_symbol *) addr2;
1650
8a6d4234
TT
1651 return (memcmp (&sym1->value, &sym2->value,
1652 sizeof (sym1->value)) == 0
1653 && sym1->language == sym2->language
1654 && sym1->domain == sym2->domain
1655 && sym1->aclass == sym2->aclass
e793c052
TT
1656 /* Note that psymbol names are interned via
1657 symbol_set_names, so there's no need to compare the
1658 contents of the name here. */
8a6d4234 1659 && sym1->name == sym2->name);
cbd70537
SW
1660}
1661
710e1a31
SW
1662/* Initialize a partial symbol bcache. */
1663
1664struct psymbol_bcache *
1665psymbol_bcache_init (void)
1666{
fc270c35 1667 struct psymbol_bcache *bcache = XCNEW (struct psymbol_bcache);
95cf5869 1668
710e1a31
SW
1669 bcache->bcache = bcache_xmalloc (psymbol_hash, psymbol_compare);
1670 return bcache;
1671}
1672
1673/* Free a partial symbol bcache. */
95cf5869 1674
710e1a31
SW
1675void
1676psymbol_bcache_free (struct psymbol_bcache *bcache)
1677{
1678 if (bcache == NULL)
1679 return;
1680
1681 bcache_xfree (bcache->bcache);
1682 xfree (bcache);
1683}
1684
0df8b418 1685/* Return the internal bcache of the psymbol_bcache BCACHE. */
710e1a31
SW
1686
1687struct bcache *
1688psymbol_bcache_get_bcache (struct psymbol_bcache *bcache)
1689{
1690 return bcache->bcache;
1691}
1692
1693/* Find a copy of the SYM in BCACHE. If BCACHE has never seen this
1694 symbol before, add a copy to BCACHE. In either case, return a pointer
1695 to BCACHE's copy of the symbol. If optional ADDED is not NULL, return
1696 1 in case of new entry or 0 if returning an old entry. */
1697
af5bf4ad 1698static struct partial_symbol *
710e1a31
SW
1699psymbol_bcache_full (struct partial_symbol *sym,
1700 struct psymbol_bcache *bcache,
1701 int *added)
1702{
af5bf4ad 1703 return ((struct partial_symbol *)
19ba03f4
SM
1704 bcache_full (sym, sizeof (struct partial_symbol), bcache->bcache,
1705 added));
710e1a31
SW
1706}
1707
95cf5869 1708/* Helper function, initialises partial symbol structure and stashes
ccefe4c4
TT
1709 it into objfile's bcache. Note that our caching mechanism will
1710 use all fields of struct partial_symbol to determine hash value of the
1711 structure. In other words, having two symbols with the same name but
1712 different domain (or address) is possible and correct. */
1713
af5bf4ad 1714static struct partial_symbol *
72b9f47f 1715add_psymbol_to_bcache (const char *name, int namelength, int copy_name,
ccefe4c4 1716 domain_enum domain,
fe978cb0 1717 enum address_class theclass,
1762568f 1718 CORE_ADDR coreaddr,
ccefe4c4
TT
1719 enum language language, struct objfile *objfile,
1720 int *added)
1721{
cbd70537
SW
1722 struct partial_symbol psymbol;
1723
02e9e7f7 1724 psymbol.set_address (coreaddr);
8a6d4234
TT
1725 psymbol.section = -1;
1726 psymbol.domain = domain;
1727 psymbol.aclass = theclass;
fc956729 1728
8a6d4234
TT
1729 memset (&psymbol.language_specific, 0, sizeof (psymbol.language_specific));
1730 psymbol.ada_mangled = 0;
1731 symbol_set_language (&psymbol, language, &objfile->objfile_obstack);
1732 symbol_set_names (&psymbol, name, namelength, copy_name, objfile);
ccefe4c4 1733
0df8b418 1734 /* Stash the partial symbol away in the cache. */
95cf5869 1735 return psymbol_bcache_full (&psymbol, objfile->psymbol_cache, added);
ccefe4c4
TT
1736}
1737
95cf5869 1738/* Helper function, adds partial symbol to the given partial symbol list. */
ccefe4c4
TT
1739
1740static void
af5bf4ad
SM
1741append_psymbol_to_list (std::vector<partial_symbol *> *list,
1742 struct partial_symbol *psym,
ccefe4c4
TT
1743 struct objfile *objfile)
1744{
af5bf4ad 1745 list->push_back (psym);
ccefe4c4
TT
1746 OBJSTAT (objfile, n_psyms++);
1747}
1748
1749/* Add a symbol with a long value to a psymtab.
1750 Since one arg is a struct, we pass in a ptr and deref it (sigh).
1762568f
DE
1751 The only value we need to store for psyms is an address.
1752 For all other psyms pass zero for COREADDR.
ccefe4c4
TT
1753 Return the partial symbol that has been added. */
1754
7dc25483 1755void
72b9f47f 1756add_psymbol_to_list (const char *name, int namelength, int copy_name,
ccefe4c4 1757 domain_enum domain,
fe978cb0 1758 enum address_class theclass,
af5bf4ad 1759 std::vector<partial_symbol *> *list,
1762568f 1760 CORE_ADDR coreaddr,
ccefe4c4
TT
1761 enum language language, struct objfile *objfile)
1762{
af5bf4ad 1763 struct partial_symbol *psym;
ccefe4c4
TT
1764
1765 int added;
1766
0df8b418 1767 /* Stash the partial symbol away in the cache. */
fe978cb0 1768 psym = add_psymbol_to_bcache (name, namelength, copy_name, domain, theclass,
1762568f 1769 coreaddr, language, objfile, &added);
ccefe4c4
TT
1770
1771 /* Do not duplicate global partial symbols. */
1772 if (list == &objfile->global_psymbols
1773 && !added)
7dc25483 1774 return;
ccefe4c4 1775
0df8b418 1776 /* Save pointer to partial symbol in psymtab, growing symtab if needed. */
ccefe4c4 1777 append_psymbol_to_list (list, psym, objfile);
ccefe4c4
TT
1778}
1779
1780/* Initialize storage for partial symbols. */
1781
1782void
1783init_psymbol_list (struct objfile *objfile, int total_symbols)
1784{
1785 /* Free any previously allocated psymbol lists. */
af5bf4ad
SM
1786 objfile->global_psymbols.clear ();
1787 objfile->static_psymbols.clear ();
ccefe4c4
TT
1788
1789 /* Current best guess is that approximately a twentieth
1790 of the total symbols (in a debugging file) are global or static
ca9c6ee2 1791 oriented symbols, then multiply that by slop factor of two. */
af5bf4ad
SM
1792 objfile->global_psymbols.reserve (total_symbols / 10);
1793 objfile->static_psymbols.reserve (total_symbols / 10);
ccefe4c4
TT
1794}
1795
1796struct partial_symtab *
1797allocate_psymtab (const char *filename, struct objfile *objfile)
1798{
1799 struct partial_symtab *psymtab;
1800
1801 if (objfile->free_psymtabs)
1802 {
1803 psymtab = objfile->free_psymtabs;
1804 objfile->free_psymtabs = psymtab->next;
1805 }
1806 else
e39db4db 1807 psymtab = XOBNEW (&objfile->objfile_obstack, partial_symtab);
ccefe4c4
TT
1808
1809 memset (psymtab, 0, sizeof (struct partial_symtab));
19ba03f4
SM
1810 psymtab->filename
1811 = (const char *) bcache (filename, strlen (filename) + 1,
1812 objfile->per_bfd->filename_cache);
43f3e411 1813 psymtab->compunit_symtab = NULL;
ccefe4c4
TT
1814
1815 /* Prepend it to the psymtab list for the objfile it belongs to.
1816 Psymtabs are searched in most recent inserted -> least recent
0df8b418 1817 inserted order. */
ccefe4c4 1818
ccefe4c4
TT
1819 psymtab->next = objfile->psymtabs;
1820 objfile->psymtabs = psymtab;
ccefe4c4 1821
45cfd468
DE
1822 if (symtab_create_debug)
1823 {
1824 /* Be a bit clever with debugging messages, and don't print objfile
1825 every time, only when it changes. */
1826 static char *last_objfile_name = NULL;
1827
1828 if (last_objfile_name == NULL
4262abfb 1829 || strcmp (last_objfile_name, objfile_name (objfile)) != 0)
45cfd468
DE
1830 {
1831 xfree (last_objfile_name);
4262abfb 1832 last_objfile_name = xstrdup (objfile_name (objfile));
45cfd468
DE
1833 fprintf_unfiltered (gdb_stdlog,
1834 "Creating one or more psymtabs for objfile %s ...\n",
1835 last_objfile_name);
1836 }
1837 fprintf_unfiltered (gdb_stdlog,
fd55216c
DE
1838 "Created psymtab %s for module %s.\n",
1839 host_address_to_string (psymtab), filename);
45cfd468
DE
1840 }
1841
95cf5869 1842 return psymtab;
ccefe4c4
TT
1843}
1844
1845void
5c80ed9d 1846discard_psymtab (struct objfile *objfile, struct partial_symtab *pst)
ccefe4c4
TT
1847{
1848 struct partial_symtab **prev_pst;
1849
1850 /* From dbxread.c:
1851 Empty psymtabs happen as a result of header files which don't
1852 have any symbols in them. There can be a lot of them. But this
1853 check is wrong, in that a psymtab with N_SLINE entries but
1854 nothing else is not empty, but we don't realize that. Fixing
1855 that without slowing things down might be tricky. */
1856
0df8b418 1857 /* First, snip it out of the psymtab chain. */
ccefe4c4 1858
5c80ed9d 1859 prev_pst = &(objfile->psymtabs);
ccefe4c4
TT
1860 while ((*prev_pst) != pst)
1861 prev_pst = &((*prev_pst)->next);
1862 (*prev_pst) = pst->next;
1863
0df8b418 1864 /* Next, put it on a free list for recycling. */
ccefe4c4 1865
5c80ed9d
TT
1866 pst->next = objfile->free_psymtabs;
1867 objfile->free_psymtabs = pst;
ccefe4c4
TT
1868}
1869
ccefe4c4
TT
1870\f
1871
372405a5
DE
1872/* We need to pass a couple of items to the addrmap_foreach function,
1873 so use a struct. */
1874
1875struct dump_psymtab_addrmap_data
1876{
1877 struct objfile *objfile;
1878 struct partial_symtab *psymtab;
1879 struct ui_file *outfile;
1880
1881 /* Non-zero if the previously printed addrmap entry was for PSYMTAB.
1882 If so, we want to print the next one as well (since the next addrmap
1883 entry defines the end of the range). */
1884 int previous_matched;
1885};
1886
1887/* Helper function for dump_psymtab_addrmap to print an addrmap entry. */
1888
1889static int
1890dump_psymtab_addrmap_1 (void *datap, CORE_ADDR start_addr, void *obj)
1891{
709b5518
PA
1892 struct dump_psymtab_addrmap_data *data
1893 = (struct dump_psymtab_addrmap_data *) datap;
372405a5 1894 struct gdbarch *gdbarch = get_objfile_arch (data->objfile);
709b5518 1895 struct partial_symtab *addrmap_psymtab = (struct partial_symtab *) obj;
372405a5
DE
1896 const char *psymtab_address_or_end = NULL;
1897
1898 QUIT;
1899
1900 if (data->psymtab == NULL
1901 || data->psymtab == addrmap_psymtab)
1902 psymtab_address_or_end = host_address_to_string (addrmap_psymtab);
1903 else if (data->previous_matched)
1904 psymtab_address_or_end = "<ends here>";
1905
1906 if (data->psymtab == NULL
1907 || data->psymtab == addrmap_psymtab
1908 || data->previous_matched)
1909 {
1910 fprintf_filtered (data->outfile, " %s%s %s\n",
1911 data->psymtab != NULL ? " " : "",
1912 paddress (gdbarch, start_addr),
1913 psymtab_address_or_end);
1914 }
1915
1916 data->previous_matched = (data->psymtab == NULL
1917 || data->psymtab == addrmap_psymtab);
1918
1919 return 0;
1920}
1921
1922/* Helper function for maintenance_print_psymbols to print the addrmap
1923 of PSYMTAB. If PSYMTAB is NULL print the entire addrmap. */
1924
1925static void
1926dump_psymtab_addrmap (struct objfile *objfile, struct partial_symtab *psymtab,
1927 struct ui_file *outfile)
1928{
1929 struct dump_psymtab_addrmap_data addrmap_dump_data;
1930
34c41c68
DE
1931 if ((psymtab == NULL
1932 || psymtab->psymtabs_addrmap_supported)
1933 && objfile->psymtabs_addrmap != NULL)
372405a5
DE
1934 {
1935 addrmap_dump_data.objfile = objfile;
1936 addrmap_dump_data.psymtab = psymtab;
1937 addrmap_dump_data.outfile = outfile;
1938 addrmap_dump_data.previous_matched = 0;
1939 fprintf_filtered (outfile, "%sddress map:\n",
1940 psymtab == NULL ? "Entire a" : " A");
1941 addrmap_foreach (objfile->psymtabs_addrmap, dump_psymtab_addrmap_1,
1942 &addrmap_dump_data);
1943 }
1944}
1945
dfc7bb5b 1946static void
990b9f9f 1947maintenance_print_psymbols (const char *args, int from_tty)
ccefe4c4 1948{
34c41c68 1949 struct ui_file *outfile = gdb_stdout;
34c41c68 1950 char *address_arg = NULL, *source_arg = NULL, *objfile_arg = NULL;
ccefe4c4
TT
1951 struct objfile *objfile;
1952 struct partial_symtab *ps;
34c41c68
DE
1953 int i, outfile_idx, found;
1954 CORE_ADDR pc = 0;
1955 struct obj_section *section = NULL;
ccefe4c4
TT
1956
1957 dont_repeat ();
1958
773a1edc 1959 gdb_argv argv (args);
ccefe4c4 1960
99e8a4f9 1961 for (i = 0; argv != NULL && argv[i] != NULL; ++i)
ccefe4c4 1962 {
34c41c68
DE
1963 if (strcmp (argv[i], "-pc") == 0)
1964 {
1965 if (argv[i + 1] == NULL)
1966 error (_("Missing pc value"));
1967 address_arg = argv[++i];
1968 }
1969 else if (strcmp (argv[i], "-source") == 0)
1970 {
1971 if (argv[i + 1] == NULL)
1972 error (_("Missing source file"));
1973 source_arg = argv[++i];
1974 }
1975 else if (strcmp (argv[i], "-objfile") == 0)
1976 {
1977 if (argv[i + 1] == NULL)
1978 error (_("Missing objfile name"));
1979 objfile_arg = argv[++i];
1980 }
1981 else if (strcmp (argv[i], "--") == 0)
1982 {
1983 /* End of options. */
1984 ++i;
1985 break;
1986 }
1987 else if (argv[i][0] == '-')
ccefe4c4 1988 {
34c41c68
DE
1989 /* Future proofing: Don't allow OUTFILE to begin with "-". */
1990 error (_("Unknown option: %s"), argv[i]);
ccefe4c4 1991 }
34c41c68
DE
1992 else
1993 break;
ccefe4c4 1994 }
34c41c68 1995 outfile_idx = i;
ccefe4c4 1996
34c41c68
DE
1997 if (address_arg != NULL && source_arg != NULL)
1998 error (_("Must specify at most one of -pc and -source"));
ccefe4c4 1999
d7e74731
PA
2000 stdio_file arg_outfile;
2001
99e8a4f9 2002 if (argv != NULL && argv[outfile_idx] != NULL)
34c41c68 2003 {
34c41c68
DE
2004 if (argv[outfile_idx + 1] != NULL)
2005 error (_("Junk at end of command"));
ee0c3293
TT
2006 gdb::unique_xmalloc_ptr<char> outfile_name
2007 (tilde_expand (argv[outfile_idx]));
2008 if (!arg_outfile.open (outfile_name.get (), FOPEN_WT))
2009 perror_with_name (outfile_name.get ());
d7e74731 2010 outfile = &arg_outfile;
34c41c68 2011 }
ccefe4c4 2012
34c41c68
DE
2013 if (address_arg != NULL)
2014 {
2015 pc = parse_and_eval_address (address_arg);
2016 /* If we fail to find a section, that's ok, try the lookup anyway. */
2017 section = find_pc_section (pc);
2018 }
372405a5 2019
34c41c68
DE
2020 found = 0;
2021 ALL_OBJFILES (objfile)
27618ce4 2022 {
34c41c68
DE
2023 int printed_objfile_header = 0;
2024 int print_for_objfile = 1;
2025
27618ce4 2026 QUIT;
34c41c68
DE
2027 if (objfile_arg != NULL)
2028 print_for_objfile
2029 = compare_filenames_for_search (objfile_name (objfile),
2030 objfile_arg);
2031 if (!print_for_objfile)
2032 continue;
2033
2034 if (address_arg != NULL)
2035 {
2036 struct bound_minimal_symbol msymbol = { NULL, NULL };
2037
2038 /* We don't assume each pc has a unique objfile (this is for
2039 debugging). */
2040 ps = find_pc_sect_psymtab (objfile, pc, section, msymbol);
2041 if (ps != NULL)
2042 {
2043 if (!printed_objfile_header)
2044 {
d7e74731
PA
2045 outfile->printf ("\nPartial symtabs for objfile %s\n",
2046 objfile_name (objfile));
34c41c68
DE
2047 printed_objfile_header = 1;
2048 }
2049 dump_psymtab (objfile, ps, outfile);
2050 dump_psymtab_addrmap (objfile, ps, outfile);
2051 found = 1;
2052 }
2053 }
2054 else
2055 {
2056 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
2057 {
2058 int print_for_source = 0;
2059
2060 QUIT;
2061 if (source_arg != NULL)
2062 {
2063 print_for_source
2064 = compare_filenames_for_search (ps->filename, source_arg);
2065 found = 1;
2066 }
2067 if (source_arg == NULL
2068 || print_for_source)
2069 {
2070 if (!printed_objfile_header)
2071 {
d7e74731
PA
2072 outfile->printf ("\nPartial symtabs for objfile %s\n",
2073 objfile_name (objfile));
34c41c68
DE
2074 printed_objfile_header = 1;
2075 }
2076 dump_psymtab (objfile, ps, outfile);
2077 dump_psymtab_addrmap (objfile, ps, outfile);
2078 }
2079 }
2080 }
2081
2082 /* If we're printing all the objfile's symbols dump the full addrmap. */
2083
2084 if (address_arg == NULL
2085 && source_arg == NULL
2086 && objfile->psymtabs_addrmap != NULL)
372405a5 2087 {
d7e74731 2088 outfile->puts ("\n");
34c41c68 2089 dump_psymtab_addrmap (objfile, NULL, outfile);
372405a5 2090 }
27618ce4 2091 }
372405a5 2092
34c41c68
DE
2093 if (!found)
2094 {
2095 if (address_arg != NULL)
2096 error (_("No partial symtab for address: %s"), address_arg);
2097 if (source_arg != NULL)
2098 error (_("No partial symtab for source file: %s"), source_arg);
2099 }
ccefe4c4
TT
2100}
2101
2102/* List all the partial symbol tables whose names match REGEXP (optional). */
95cf5869 2103
dfc7bb5b 2104static void
990b9f9f 2105maintenance_info_psymtabs (const char *regexp, int from_tty)
ccefe4c4
TT
2106{
2107 struct program_space *pspace;
2108 struct objfile *objfile;
2109
2110 if (regexp)
2111 re_comp (regexp);
2112
2113 ALL_PSPACES (pspace)
2114 ALL_PSPACE_OBJFILES (pspace, objfile)
2115 {
2116 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2117 struct partial_symtab *psymtab;
2118
2119 /* We don't want to print anything for this objfile until we
2120 actually find a symtab whose name matches. */
2121 int printed_objfile_start = 0;
2122
b11896a5 2123 ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, psymtab)
ccefe4c4
TT
2124 {
2125 QUIT;
2126
2127 if (! regexp
2128 || re_exec (psymtab->filename))
2129 {
2130 if (! printed_objfile_start)
2131 {
4262abfb 2132 printf_filtered ("{ objfile %s ", objfile_name (objfile));
ccefe4c4 2133 wrap_here (" ");
95cf5869 2134 printf_filtered ("((struct objfile *) %s)\n",
ccefe4c4
TT
2135 host_address_to_string (objfile));
2136 printed_objfile_start = 1;
2137 }
2138
2139 printf_filtered (" { psymtab %s ", psymtab->filename);
2140 wrap_here (" ");
95cf5869 2141 printf_filtered ("((struct partial_symtab *) %s)\n",
ccefe4c4
TT
2142 host_address_to_string (psymtab));
2143
2144 printf_filtered (" readin %s\n",
2145 psymtab->readin ? "yes" : "no");
2146 printf_filtered (" fullname %s\n",
3e43a32a
MS
2147 psymtab->fullname
2148 ? psymtab->fullname : "(null)");
ccefe4c4
TT
2149 printf_filtered (" text addresses ");
2150 fputs_filtered (paddress (gdbarch, psymtab->textlow),
2151 gdb_stdout);
2152 printf_filtered (" -- ");
2153 fputs_filtered (paddress (gdbarch, psymtab->texthigh),
2154 gdb_stdout);
2155 printf_filtered ("\n");
9750bca9
JK
2156 printf_filtered (" psymtabs_addrmap_supported %s\n",
2157 (psymtab->psymtabs_addrmap_supported
2158 ? "yes" : "no"));
ccefe4c4
TT
2159 printf_filtered (" globals ");
2160 if (psymtab->n_global_syms)
2161 {
af5bf4ad
SM
2162 auto p = &objfile->global_psymbols[psymtab->globals_offset];
2163
ccefe4c4 2164 printf_filtered ("(* (struct partial_symbol **) %s @ %d)\n",
af5bf4ad 2165 host_address_to_string (p),
ccefe4c4
TT
2166 psymtab->n_global_syms);
2167 }
2168 else
2169 printf_filtered ("(none)\n");
2170 printf_filtered (" statics ");
2171 if (psymtab->n_static_syms)
2172 {
af5bf4ad
SM
2173 auto p = &objfile->static_psymbols[psymtab->statics_offset];
2174
ccefe4c4 2175 printf_filtered ("(* (struct partial_symbol **) %s @ %d)\n",
af5bf4ad 2176 host_address_to_string (p),
ccefe4c4
TT
2177 psymtab->n_static_syms);
2178 }
2179 else
2180 printf_filtered ("(none)\n");
2181 printf_filtered (" dependencies ");
2182 if (psymtab->number_of_dependencies)
2183 {
2184 int i;
2185
2186 printf_filtered ("{\n");
2187 for (i = 0; i < psymtab->number_of_dependencies; i++)
2188 {
2189 struct partial_symtab *dep = psymtab->dependencies[i];
2190
2191 /* Note the string concatenation there --- no comma. */
2192 printf_filtered (" psymtab %s "
2193 "((struct partial_symtab *) %s)\n",
95cf5869 2194 dep->filename,
ccefe4c4
TT
2195 host_address_to_string (dep));
2196 }
2197 printf_filtered (" }\n");
2198 }
2199 else
2200 printf_filtered ("(none)\n");
2201 printf_filtered (" }\n");
2202 }
2203 }
2204
2205 if (printed_objfile_start)
2206 printf_filtered ("}\n");
2207 }
2208}
2209
7d0c9981 2210/* Check consistency of currently expanded psymtabs vs symtabs. */
ccefe4c4 2211
dfc7bb5b 2212static void
990b9f9f 2213maintenance_check_psymtabs (const char *ignore, int from_tty)
ccefe4c4
TT
2214{
2215 struct symbol *sym;
43f3e411 2216 struct compunit_symtab *cust = NULL;
ccefe4c4 2217 struct partial_symtab *ps;
346d1dfe 2218 const struct blockvector *bv;
ccefe4c4
TT
2219 struct objfile *objfile;
2220 struct block *b;
2221 int length;
2222
2223 ALL_PSYMTABS (objfile, ps)
2224 {
2225 struct gdbarch *gdbarch = get_objfile_arch (objfile);
ad3bbd48 2226
7d0c9981
DE
2227 /* We don't call psymtab_to_symtab here because that may cause symtab
2228 expansion. When debugging a problem it helps if checkers leave
2229 things unchanged. */
43f3e411 2230 cust = ps->compunit_symtab;
7d0c9981
DE
2231
2232 /* First do some checks that don't require the associated symtab. */
2233 if (ps->texthigh < ps->textlow)
2234 {
2235 printf_filtered ("Psymtab ");
2236 puts_filtered (ps->filename);
2237 printf_filtered (" covers bad range ");
2238 fputs_filtered (paddress (gdbarch, ps->textlow), gdb_stdout);
2239 printf_filtered (" - ");
2240 fputs_filtered (paddress (gdbarch, ps->texthigh), gdb_stdout);
2241 printf_filtered ("\n");
2242 continue;
2243 }
2244
2245 /* Now do checks requiring the associated symtab. */
43f3e411 2246 if (cust == NULL)
ccefe4c4 2247 continue;
43f3e411 2248 bv = COMPUNIT_BLOCKVECTOR (cust);
ccefe4c4 2249 b = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
af5bf4ad 2250 partial_symbol **psym = &objfile->static_psymbols[ps->statics_offset];
ccefe4c4
TT
2251 length = ps->n_static_syms;
2252 while (length--)
2253 {
8a6d4234 2254 sym = block_lookup_symbol (b, symbol_search_name (*psym),
de63c46b 2255 symbol_name_match_type::SEARCH_NAME,
8a6d4234 2256 (*psym)->domain);
ccefe4c4
TT
2257 if (!sym)
2258 {
2259 printf_filtered ("Static symbol `");
8a6d4234 2260 puts_filtered ((*psym)->name);
ccefe4c4
TT
2261 printf_filtered ("' only found in ");
2262 puts_filtered (ps->filename);
2263 printf_filtered (" psymtab\n");
2264 }
2265 psym++;
2266 }
2267 b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
af5bf4ad 2268 psym = &objfile->global_psymbols[ps->globals_offset];
ccefe4c4
TT
2269 length = ps->n_global_syms;
2270 while (length--)
2271 {
8a6d4234 2272 sym = block_lookup_symbol (b, symbol_search_name (*psym),
de63c46b 2273 symbol_name_match_type::SEARCH_NAME,
8a6d4234 2274 (*psym)->domain);
ccefe4c4
TT
2275 if (!sym)
2276 {
2277 printf_filtered ("Global symbol `");
8a6d4234 2278 puts_filtered ((*psym)->name);
ccefe4c4
TT
2279 printf_filtered ("' only found in ");
2280 puts_filtered (ps->filename);
2281 printf_filtered (" psymtab\n");
2282 }
2283 psym++;
2284 }
7d0c9981
DE
2285 if (ps->texthigh != 0
2286 && (ps->textlow < BLOCK_START (b) || ps->texthigh > BLOCK_END (b)))
ccefe4c4
TT
2287 {
2288 printf_filtered ("Psymtab ");
2289 puts_filtered (ps->filename);
2290 printf_filtered (" covers ");
2291 fputs_filtered (paddress (gdbarch, ps->textlow), gdb_stdout);
2292 printf_filtered (" - ");
2293 fputs_filtered (paddress (gdbarch, ps->texthigh), gdb_stdout);
2294 printf_filtered (" but symtab covers only ");
2295 fputs_filtered (paddress (gdbarch, BLOCK_START (b)), gdb_stdout);
2296 printf_filtered (" - ");
2297 fputs_filtered (paddress (gdbarch, BLOCK_END (b)), gdb_stdout);
2298 printf_filtered ("\n");
2299 }
2300 }
2301}
2302
dfc7bb5b
YQ
2303void
2304_initialize_psymtab (void)
2305{
2306 add_cmd ("psymbols", class_maintenance, maintenance_print_psymbols, _("\
2307Print dump of current partial symbol definitions.\n\
34c41c68
DE
2308Usage: mt print psymbols [-objfile objfile] [-pc address] [--] [outfile]\n\
2309 mt print psymbols [-objfile objfile] [-source source] [--] [outfile]\n\
2310Entries in the partial symbol table are dumped to file OUTFILE,\n\
2311or the terminal if OUTFILE is unspecified.\n\
2312If ADDRESS is provided, dump only the file for that address.\n\
2313If SOURCE is provided, dump only that file's symbols.\n\
2314If OBJFILE is provided, dump only that file's minimal symbols."),
dfc7bb5b
YQ
2315 &maintenanceprintlist);
2316
2317 add_cmd ("psymtabs", class_maintenance, maintenance_info_psymtabs, _("\
2318List the partial symbol tables for all object files.\n\
2319This does not include information about individual partial symbols,\n\
2320just the symbol table structures themselves."),
2321 &maintenanceinfolist);
2322
7d0c9981
DE
2323 add_cmd ("check-psymtabs", class_maintenance, maintenance_check_psymtabs,
2324 _("\
2325Check consistency of currently expanded psymtabs versus symtabs."),
dfc7bb5b
YQ
2326 &maintenancelist);
2327}