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