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