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