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