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