Change get_objfile_arch to a method on objfile
[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"),
08feed99 395 paddress (objfile->arch (), pc));
5c80ed9d 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
d3214198
TV
524/* Psymtab version of lookup_global_symbol_language. See its definition in
525 the definition of quick_symbol_functions in symfile.h. */
526
527static enum language
528psym_lookup_global_symbol_language (struct objfile *objfile, const char *name,
529 domain_enum domain, bool *symbol_found_p)
530{
531 *symbol_found_p = false;
532 if (objfile->sf == NULL)
533 return language_unknown;
534
535 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
536
537 for (partial_symtab *ps : require_partial_symbols (objfile, true))
538 {
539 struct partial_symbol *psym;
540 if (ps->readin_p ())
541 continue;
542
543 psym = lookup_partial_symbol (objfile, ps, lookup_name, 1, domain);
544 if (psym)
545 {
546 *symbol_found_p = true;
547 return psym->ginfo.language ();
548 }
549 }
550
551 return language_unknown;
552}
553
b5ec771e
PA
554/* Returns true if PSYM matches LOOKUP_NAME. */
555
556static bool
557psymbol_name_matches (partial_symbol *psym,
558 const lookup_name_info &lookup_name)
559{
c1b5c1eb 560 const language_defn *lang = language_def (psym->ginfo.language ());
b5ec771e 561 symbol_name_matcher_ftype *name_match
618daa93 562 = get_symbol_name_matcher (lang, lookup_name);
c9d95fa3 563 return name_match (psym->ginfo.search_name (), lookup_name, NULL);
b5ec771e
PA
564}
565
40658b94
PH
566/* Look in PST for a symbol in DOMAIN whose name matches NAME. Search
567 the global block of PST if GLOBAL, and otherwise the static block.
568 MATCH is the comparison operation that returns true iff MATCH (s,
569 NAME), where s is a SYMBOL_SEARCH_NAME. If ORDERED_COMPARE is
570 non-null, the symbols in the block are assumed to be ordered
571 according to it (allowing binary search). It must be compatible
572 with MATCH. Returns the symbol, if found, and otherwise NULL. */
573
574static struct partial_symbol *
5c80ed9d
TT
575match_partial_symbol (struct objfile *objfile,
576 struct partial_symtab *pst, int global,
b054970d 577 const lookup_name_info &name, domain_enum domain,
2edb89d3 578 symbol_compare_ftype *ordered_compare)
40658b94
PH
579{
580 struct partial_symbol **start, **psym;
581 struct partial_symbol **top, **real_top, **bottom, **center;
582 int length = (global ? pst->n_global_syms : pst->n_static_syms);
583 int do_linear_search = 1;
584
585 if (length == 0)
b5ec771e
PA
586 return NULL;
587
40658b94 588 start = (global ?
d320c2b5
TT
589 &objfile->partial_symtabs->global_psymbols[pst->globals_offset] :
590 &objfile->partial_symtabs->static_psymbols[pst->statics_offset]);
40658b94
PH
591
592 if (global && ordered_compare) /* Can use a binary search. */
593 {
594 do_linear_search = 0;
595
596 /* Binary search. This search is guaranteed to end with center
597 pointing at the earliest partial symbol whose name might be
598 correct. At that point *all* partial symbols with an
599 appropriate name will be checked against the correct
600 domain. */
601
602 bottom = start;
603 top = start + length - 1;
604 real_top = top;
605 while (top > bottom)
606 {
607 center = bottom + (top - bottom) / 2;
608 gdb_assert (center < top);
b5ec771e 609
c1b5c1eb 610 enum language lang = (*center)->ginfo.language ();
e0802d59 611 const char *lang_ln = name.language_lookup_name (lang);
b5ec771e 612
c9d95fa3 613 if (ordered_compare ((*center)->ginfo.search_name (),
af97b416 614 lang_ln) >= 0)
40658b94
PH
615 top = center;
616 else
617 bottom = center + 1;
618 }
619 gdb_assert (top == bottom);
620
621 while (top <= real_top
b054970d 622 && psymbol_name_matches (*top, name))
40658b94 623 {
c1b5c1eb 624 if (symbol_matches_domain ((*top)->ginfo.language (),
8a6d4234 625 (*top)->domain, domain))
40658b94
PH
626 return *top;
627 top++;
628 }
629 }
630
631 /* Can't use a binary search or else we found during the binary search that
632 we should also do a linear search. */
633
634 if (do_linear_search)
635 {
636 for (psym = start; psym < start + length; psym++)
637 {
c1b5c1eb 638 if (symbol_matches_domain ((*psym)->ginfo.language (),
8a6d4234 639 (*psym)->domain, domain)
b054970d 640 && psymbol_name_matches (*psym, name))
40658b94
PH
641 return *psym;
642 }
643 }
644
645 return NULL;
646}
647
8c072cb6
TT
648/* Look, in partial_symtab PST, for symbol whose natural name is
649 LOOKUP_NAME. Check the global symbols if GLOBAL, the static
650 symbols if not. */
ccefe4c4 651
18430289 652static struct partial_symbol *
5c80ed9d 653lookup_partial_symbol (struct objfile *objfile,
8c072cb6
TT
654 struct partial_symtab *pst,
655 const lookup_name_info &lookup_name,
ccefe4c4
TT
656 int global, domain_enum domain)
657{
ccefe4c4
TT
658 struct partial_symbol **start, **psym;
659 struct partial_symbol **top, **real_top, **bottom, **center;
660 int length = (global ? pst->n_global_syms : pst->n_static_syms);
661 int do_linear_search = 1;
662
663 if (length == 0)
95cf5869 664 return NULL;
c00f8484 665
ccefe4c4 666 start = (global ?
d320c2b5
TT
667 &objfile->partial_symtabs->global_psymbols[pst->globals_offset] :
668 &objfile->partial_symtabs->static_psymbols[pst->statics_offset]);
ccefe4c4 669
0df8b418 670 if (global) /* This means we can use a binary search. */
ccefe4c4
TT
671 {
672 do_linear_search = 0;
673
674 /* Binary search. This search is guaranteed to end with center
675 pointing at the earliest partial symbol whose name might be
676 correct. At that point *all* partial symbols with an
677 appropriate name will be checked against the correct
678 domain. */
679
680 bottom = start;
681 top = start + length - 1;
682 real_top = top;
683 while (top > bottom)
684 {
685 center = bottom + (top - bottom) / 2;
e2ff18a0
SM
686
687 gdb_assert (center < top);
688
c9d95fa3 689 if (strcmp_iw_ordered ((*center)->ginfo.search_name (),
e0802d59 690 lookup_name.c_str ()) >= 0)
ccefe4c4
TT
691 {
692 top = center;
693 }
694 else
695 {
696 bottom = center + 1;
697 }
698 }
e2ff18a0
SM
699
700 gdb_assert (top == bottom);
ccefe4c4 701
559a7a62
JK
702 /* For `case_sensitivity == case_sensitive_off' strcmp_iw_ordered will
703 search more exactly than what matches SYMBOL_MATCHES_SEARCH_NAME. */
af97b416
TT
704 while (top >= start && symbol_matches_search_name (&(*top)->ginfo,
705 lookup_name))
559a7a62
JK
706 top--;
707
708 /* Fixup to have a symbol which matches SYMBOL_MATCHES_SEARCH_NAME. */
709 top++;
710
af97b416
TT
711 while (top <= real_top && symbol_matches_search_name (&(*top)->ginfo,
712 lookup_name))
ccefe4c4 713 {
c1b5c1eb 714 if (symbol_matches_domain ((*top)->ginfo.language (),
8a6d4234 715 (*top)->domain, domain))
56f37645 716 return *top;
ccefe4c4
TT
717 top++;
718 }
719 }
720
721 /* Can't use a binary search or else we found during the binary search that
40658b94 722 we should also do a linear search. */
ccefe4c4
TT
723
724 if (do_linear_search)
725 {
726 for (psym = start; psym < start + length; psym++)
727 {
c1b5c1eb 728 if (symbol_matches_domain ((*psym)->ginfo.language (),
8a6d4234 729 (*psym)->domain, domain)
af97b416 730 && symbol_matches_search_name (&(*psym)->ginfo, lookup_name))
56f37645 731 return *psym;
ccefe4c4
TT
732 }
733 }
734
95cf5869 735 return NULL;
ccefe4c4
TT
736}
737
738/* Get the symbol table that corresponds to a partial_symtab.
f194fefb
DE
739 This is fast after the first time you do it.
740 The result will be NULL if the primary symtab has no symbols,
741 which can happen. Otherwise the result is the primary symtab
742 that contains PST. */
ccefe4c4 743
43f3e411 744static struct compunit_symtab *
5c80ed9d 745psymtab_to_symtab (struct objfile *objfile, struct partial_symtab *pst)
ccefe4c4 746{
9439a077
TT
747 /* If it is a shared psymtab, find an unshared psymtab that includes
748 it. Any such psymtab will do. */
749 while (pst->user != NULL)
750 pst = pst->user;
751
0df8b418 752 /* If it's been looked up before, return it. */
128a391f
TT
753 if (pst->get_compunit_symtab ())
754 return pst->get_compunit_symtab ();
ccefe4c4
TT
755
756 /* If it has not yet been read in, read it. */
128a391f 757 if (!pst->readin_p ())
ccefe4c4 758 {
c83dd867 759 scoped_restore decrementer = increment_reading_symtab ();
ad3bbd48 760
077cbab2
TT
761 if (info_verbose)
762 {
fd760e79 763 printf_filtered (_("Reading in symbols for %s...\n"),
077cbab2
TT
764 pst->filename);
765 gdb_flush (gdb_stdout);
766 }
767
891813be 768 pst->read_symtab (objfile);
ccefe4c4
TT
769 }
770
128a391f 771 return pst->get_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 {
128a391f 794 if (cs_pst->readin_p ())
ccefe4c4
TT
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{
08feed99 930 struct gdbarch *gdbarch = objfile->arch ();
ccefe4c4
TT
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 950
128a391f 951 if (psymtab->readin_p ())
ccefe4c4
TT
952 {
953 fprintf_filtered (outfile,
954 " Full symtab was read (at ");
128a391f 955 gdb_print_host_address (psymtab->get_compunit_symtab (), outfile);
ccefe4c4
TT
956 fprintf_filtered (outfile, ")\n");
957 }
958
ccefe4c4 959 fprintf_filtered (outfile, " Symbols cover text addresses ");
79748972 960 fputs_filtered (paddress (gdbarch, psymtab->text_low (objfile)), outfile);
ccefe4c4 961 fprintf_filtered (outfile, "-");
79748972 962 fputs_filtered (paddress (gdbarch, psymtab->text_high (objfile)), outfile);
ccefe4c4 963 fprintf_filtered (outfile, "\n");
9750bca9
JK
964 fprintf_filtered (outfile, " Address map supported - %s.\n",
965 psymtab->psymtabs_addrmap_supported ? "yes" : "no");
ccefe4c4
TT
966 fprintf_filtered (outfile, " Depends on %d other partial symtabs.\n",
967 psymtab->number_of_dependencies);
968 for (i = 0; i < psymtab->number_of_dependencies; i++)
969 {
970 fprintf_filtered (outfile, " %d ", i);
971 gdb_print_host_address (psymtab->dependencies[i], outfile);
972 fprintf_filtered (outfile, " %s\n",
973 psymtab->dependencies[i]->filename);
974 }
9439a077
TT
975 if (psymtab->user != NULL)
976 {
977 fprintf_filtered (outfile, " Shared partial symtab with user ");
978 gdb_print_host_address (psymtab->user, outfile);
979 fprintf_filtered (outfile, "\n");
980 }
ccefe4c4
TT
981 if (psymtab->n_global_syms > 0)
982 {
d320c2b5
TT
983 print_partial_symbols
984 (gdbarch, objfile,
985 &objfile->partial_symtabs->global_psymbols[psymtab->globals_offset],
986 psymtab->n_global_syms, "Global", outfile);
ccefe4c4
TT
987 }
988 if (psymtab->n_static_syms > 0)
989 {
d320c2b5
TT
990 print_partial_symbols
991 (gdbarch, objfile,
992 &objfile->partial_symtabs->static_psymbols[psymtab->statics_offset],
993 psymtab->n_static_syms, "Static", outfile);
ccefe4c4
TT
994 }
995 fprintf_filtered (outfile, "\n");
996}
997
95cf5869
DE
998/* Psymtab version of print_stats. See its definition in
999 the definition of quick_symbol_functions in symfile.h. */
83827540 1000
ccefe4c4 1001static void
83827540 1002psym_print_stats (struct objfile *objfile)
ccefe4c4
TT
1003{
1004 int i;
ad3bbd48 1005
ccefe4c4 1006 i = 0;
26abc753 1007 for (partial_symtab *ps : require_partial_symbols (objfile, true))
ccefe4c4 1008 {
128a391f 1009 if (!ps->readin_p ())
ccefe4c4
TT
1010 i++;
1011 }
1012 printf_filtered (_(" Number of psym tables (not yet expanded): %d\n"), i);
1013}
1014
95cf5869
DE
1015/* Psymtab version of dump. See its definition in
1016 the definition of quick_symbol_functions in symfile.h. */
83827540 1017
ccefe4c4 1018static void
83827540 1019psym_dump (struct objfile *objfile)
ccefe4c4
TT
1020{
1021 struct partial_symtab *psymtab;
1022
d320c2b5 1023 if (objfile->partial_symtabs->psymtabs)
ccefe4c4
TT
1024 {
1025 printf_filtered ("Psymtabs:\n");
d320c2b5 1026 for (psymtab = objfile->partial_symtabs->psymtabs;
ccefe4c4
TT
1027 psymtab != NULL;
1028 psymtab = psymtab->next)
1029 {
1030 printf_filtered ("%s at ",
1031 psymtab->filename);
1032 gdb_print_host_address (psymtab, gdb_stdout);
1033 printf_filtered (", ");
ccefe4c4
TT
1034 wrap_here (" ");
1035 }
1036 printf_filtered ("\n\n");
1037 }
1038}
1039
95cf5869
DE
1040/* Psymtab version of expand_symtabs_for_function. See its definition in
1041 the definition of quick_symbol_functions in symfile.h. */
ccefe4c4
TT
1042
1043static void
83827540
DE
1044psym_expand_symtabs_for_function (struct objfile *objfile,
1045 const char *func_name)
ccefe4c4 1046{
8c072cb6
TT
1047 lookup_name_info base_lookup (func_name, symbol_name_match_type::FULL);
1048 lookup_name_info lookup_name = base_lookup.make_ignore_params ();
1049
26abc753 1050 for (partial_symtab *ps : require_partial_symbols (objfile, true))
b22a7c6a 1051 {
128a391f 1052 if (ps->readin_p ())
b22a7c6a 1053 continue;
ccefe4c4 1054
8c072cb6 1055 if ((lookup_partial_symbol (objfile, ps, lookup_name, 1, VAR_DOMAIN)
b22a7c6a 1056 != NULL)
8c072cb6 1057 || (lookup_partial_symbol (objfile, ps, lookup_name, 0, VAR_DOMAIN)
b22a7c6a
TT
1058 != NULL))
1059 psymtab_to_symtab (objfile, ps);
1060 }
ccefe4c4
TT
1061}
1062
95cf5869
DE
1063/* Psymtab version of expand_all_symtabs. See its definition in
1064 the definition of quick_symbol_functions in symfile.h. */
83827540 1065
ccefe4c4 1066static void
83827540 1067psym_expand_all_symtabs (struct objfile *objfile)
ccefe4c4 1068{
26abc753 1069 for (partial_symtab *psymtab : require_partial_symbols (objfile, true))
b22a7c6a 1070 psymtab_to_symtab (objfile, psymtab);
ccefe4c4
TT
1071}
1072
95cf5869
DE
1073/* Psymtab version of expand_symtabs_with_fullname. See its definition in
1074 the definition of quick_symbol_functions in symfile.h. */
83827540 1075
ccefe4c4 1076static void
83827540
DE
1077psym_expand_symtabs_with_fullname (struct objfile *objfile,
1078 const char *fullname)
ccefe4c4 1079{
26abc753 1080 for (partial_symtab *p : require_partial_symbols (objfile, true))
ccefe4c4 1081 {
b4c41fc7
DE
1082 /* Anonymous psymtabs don't have a name of a source file. */
1083 if (p->anonymous)
1084 continue;
1085
5ff888ce
DE
1086 /* psymtab_to_fullname tries to open the file which is slow.
1087 Don't call it if we know the basenames don't match. */
1088 if ((basenames_may_differ
1089 || filename_cmp (lbasename (fullname), lbasename (p->filename)) == 0)
1090 && filename_cmp (fullname, psymtab_to_fullname (p)) == 0)
5c80ed9d 1091 psymtab_to_symtab (objfile, p);
ccefe4c4
TT
1092 }
1093}
1094
95cf5869
DE
1095/* Psymtab version of map_symbol_filenames. See its definition in
1096 the definition of quick_symbol_functions in symfile.h. */
83827540 1097
ccefe4c4 1098static void
83827540
DE
1099psym_map_symbol_filenames (struct objfile *objfile,
1100 symbol_filename_ftype *fun, void *data,
1101 int need_fullname)
ccefe4c4 1102{
26abc753 1103 for (partial_symtab *ps : require_partial_symbols (objfile, true))
ccefe4c4
TT
1104 {
1105 const char *fullname;
1106
128a391f 1107 if (ps->readin_p ())
ccefe4c4
TT
1108 continue;
1109
f80c6f3f
DE
1110 /* We can skip shared psymtabs here, because any file name will be
1111 attached to the unshared psymtab. */
1112 if (ps->user != NULL)
1113 continue;
1114
b4c41fc7
DE
1115 /* Anonymous psymtabs don't have a file name. */
1116 if (ps->anonymous)
1117 continue;
1118
821296b7 1119 QUIT;
74e2f255
DE
1120 if (need_fullname)
1121 fullname = psymtab_to_fullname (ps);
1122 else
1123 fullname = NULL;
2837d59e 1124 (*fun) (ps->filename, fullname, data);
ccefe4c4
TT
1125 }
1126}
1127
ccefe4c4
TT
1128/* Finds the fullname that a partial_symtab represents.
1129
1130 If this functions finds the fullname, it will save it in ps->fullname
1131 and it will also return the value.
1132
1133 If this function fails to find the file that this partial_symtab represents,
1134 NULL will be returned and ps->fullname will be set to NULL. */
256f06f3 1135
da5132d3 1136static const char *
ccefe4c4
TT
1137psymtab_to_fullname (struct partial_symtab *ps)
1138{
fbd9ab74 1139 gdb_assert (!ps->anonymous);
ccefe4c4 1140
256f06f3
DE
1141 /* Use cached copy if we have it.
1142 We rely on forget_cached_source_info being called appropriately
1143 to handle cases like the file being moved. */
fbd9ab74
JK
1144 if (ps->fullname == NULL)
1145 {
e0cc99a6 1146 gdb::unique_xmalloc_ptr<char> fullname;
2179fbc3
TT
1147 scoped_fd fd = find_and_open_source (ps->filename, ps->dirname,
1148 &fullname);
e0cc99a6 1149 ps->fullname = fullname.release ();
256f06f3 1150
2179fbc3 1151 if (fd.get () < 0)
fbd9ab74 1152 {
fbd9ab74
JK
1153 /* rewrite_source_path would be applied by find_and_open_source, we
1154 should report the pathname where GDB tried to find the file. */
ccefe4c4 1155
fbd9ab74 1156 if (ps->dirname == NULL || IS_ABSOLUTE_PATH (ps->filename))
0b581c69 1157 fullname.reset (xstrdup (ps->filename));
fbd9ab74 1158 else
0b581c69
TT
1159 fullname.reset (concat (ps->dirname, SLASH_STRING,
1160 ps->filename, (char *) NULL));
fbd9ab74 1161
0b581c69 1162 ps->fullname = rewrite_source_path (fullname.get ()).release ();
fbd9ab74 1163 if (ps->fullname == NULL)
0b581c69 1164 ps->fullname = fullname.release ();
fbd9ab74 1165 }
95cf5869 1166 }
fbd9ab74
JK
1167
1168 return ps->fullname;
ccefe4c4
TT
1169}
1170
95cf5869
DE
1171/* Psymtab version of map_matching_symbols. See its definition in
1172 the definition of quick_symbol_functions in symfile.h. */
40658b94 1173
ccefe4c4 1174static void
199b4314
TT
1175psym_map_matching_symbols
1176 (struct objfile *objfile,
b054970d 1177 const lookup_name_info &name, domain_enum domain,
199b4314
TT
1178 int global,
1179 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 1180 symbol_compare_ftype *ordered_compare)
ccefe4c4 1181{
40658b94 1182 const int block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
ccefe4c4 1183
26abc753 1184 for (partial_symtab *ps : require_partial_symbols (objfile, true))
ccefe4c4
TT
1185 {
1186 QUIT;
128a391f 1187 if (ps->readin_p ()
b054970d 1188 || match_partial_symbol (objfile, ps, global, name, domain,
40658b94 1189 ordered_compare))
ccefe4c4 1190 {
43f3e411 1191 struct compunit_symtab *cust = psymtab_to_symtab (objfile, ps);
582942f4 1192 const struct block *block;
ad3bbd48 1193
43f3e411 1194 if (cust == NULL)
ccefe4c4 1195 continue;
43f3e411 1196 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
b054970d 1197 if (!iterate_over_symbols_terminated (block, name,
6a3dbf1b 1198 domain, callback))
40658b94 1199 return;
ccefe4c4
TT
1200 }
1201 }
95cf5869 1202}
ccefe4c4 1203
14bc53a8
PA
1204/* A helper for psym_expand_symtabs_matching that handles searching
1205 included psymtabs. This returns true if a symbol is found, and
1206 false otherwise. It also updates the 'searched_flag' on the
9439a077
TT
1207 various psymtabs that it searches. */
1208
14bc53a8
PA
1209static bool
1210recursively_search_psymtabs
d2f7dcb2
JB
1211 (struct partial_symtab *ps,
1212 struct objfile *objfile,
1213 enum search_domain domain,
b5ec771e 1214 const lookup_name_info &lookup_name,
14bc53a8 1215 gdb::function_view<expand_symtabs_symbol_matcher_ftype> sym_matcher)
9439a077 1216{
9439a077 1217 int keep_going = 1;
f486487f 1218 enum psymtab_search_status result = PST_SEARCHED_AND_NOT_FOUND;
9439a077
TT
1219 int i;
1220
1221 if (ps->searched_flag != PST_NOT_SEARCHED)
1222 return ps->searched_flag == PST_SEARCHED_AND_FOUND;
1223
1224 /* Recurse into shared psymtabs first, because they may have already
1225 been searched, and this could save some time. */
1226 for (i = 0; i < ps->number_of_dependencies; ++i)
1227 {
1228 int r;
1229
1230 /* Skip non-shared dependencies, these are handled elsewhere. */
1231 if (ps->dependencies[i]->user == NULL)
1232 continue;
1233
1234 r = recursively_search_psymtabs (ps->dependencies[i],
b5ec771e
PA
1235 objfile, domain, lookup_name,
1236 sym_matcher);
9439a077
TT
1237 if (r != 0)
1238 {
1239 ps->searched_flag = PST_SEARCHED_AND_FOUND;
14bc53a8 1240 return true;
9439a077
TT
1241 }
1242 }
1243
af5bf4ad 1244 partial_symbol **gbound
d320c2b5
TT
1245 = (objfile->partial_symtabs->global_psymbols.data ()
1246 + ps->globals_offset + ps->n_global_syms);
af5bf4ad 1247 partial_symbol **sbound
d320c2b5
TT
1248 = (objfile->partial_symtabs->static_psymbols.data ()
1249 + ps->statics_offset + ps->n_static_syms);
af5bf4ad 1250 partial_symbol **bound = gbound;
9439a077
TT
1251
1252 /* Go through all of the symbols stored in a partial
1253 symtab in one loop. */
d320c2b5
TT
1254 partial_symbol **psym = (objfile->partial_symtabs->global_psymbols.data ()
1255 + ps->globals_offset);
9439a077
TT
1256 while (keep_going)
1257 {
1258 if (psym >= bound)
1259 {
1260 if (bound == gbound && ps->n_static_syms != 0)
1261 {
d320c2b5
TT
1262 psym = (objfile->partial_symtabs->static_psymbols.data ()
1263 + ps->statics_offset);
9439a077
TT
1264 bound = sbound;
1265 }
1266 else
1267 keep_going = 0;
1268 continue;
1269 }
1270 else
1271 {
1272 QUIT;
1273
b5ec771e 1274 if ((domain == ALL_DOMAIN
59c35742
AB
1275 || (domain == MODULES_DOMAIN
1276 && (*psym)->domain == MODULE_DOMAIN)
b5ec771e 1277 || (domain == VARIABLES_DOMAIN
8a6d4234
TT
1278 && (*psym)->aclass != LOC_TYPEDEF
1279 && (*psym)->aclass != LOC_BLOCK)
b5ec771e 1280 || (domain == FUNCTIONS_DOMAIN
8a6d4234 1281 && (*psym)->aclass == LOC_BLOCK)
b5ec771e 1282 || (domain == TYPES_DOMAIN
8a6d4234 1283 && (*psym)->aclass == LOC_TYPEDEF))
b5ec771e 1284 && psymbol_name_matches (*psym, lookup_name)
af97b416 1285 && (sym_matcher == NULL
c9d95fa3 1286 || sym_matcher ((*psym)->ginfo.search_name ())))
9439a077
TT
1287 {
1288 /* Found a match, so notify our caller. */
1289 result = PST_SEARCHED_AND_FOUND;
1290 keep_going = 0;
1291 }
1292 }
1293 psym++;
1294 }
1295
1296 ps->searched_flag = result;
1297 return result == PST_SEARCHED_AND_FOUND;
1298}
1299
95cf5869
DE
1300/* Psymtab version of expand_symtabs_matching. See its definition in
1301 the definition of quick_symbol_functions in symfile.h. */
83827540 1302
ccefe4c4 1303static void
83827540 1304psym_expand_symtabs_matching
f8eba3c6 1305 (struct objfile *objfile,
14bc53a8 1306 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 1307 const lookup_name_info *lookup_name,
14bc53a8
PA
1308 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
1309 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
b5ec771e 1310 enum search_domain domain)
ccefe4c4 1311{
9439a077 1312 /* Clear the search flags. */
26abc753 1313 for (partial_symtab *ps : require_partial_symbols (objfile, true))
b22a7c6a 1314 ps->searched_flag = PST_NOT_SEARCHED;
ccefe4c4 1315
f252c6d5 1316 for (partial_symtab *ps : objfile->psymtabs ())
9439a077 1317 {
30b3dd9d
DE
1318 QUIT;
1319
128a391f 1320 if (ps->readin_p ())
ccefe4c4
TT
1321 continue;
1322
9439a077
TT
1323 /* We skip shared psymtabs because file-matching doesn't apply
1324 to them; but we search them later in the loop. */
1325 if (ps->user != NULL)
ccefe4c4
TT
1326 continue;
1327
b4c41fc7
DE
1328 if (file_matcher)
1329 {
14bc53a8 1330 bool match;
680d1742 1331
b4c41fc7
DE
1332 if (ps->anonymous)
1333 continue;
fbd9ab74 1334
14bc53a8 1335 match = file_matcher (ps->filename, false);
680d1742
DE
1336 if (!match)
1337 {
1338 /* Before we invoke realpath, which can get expensive when many
1339 files are involved, do a quick comparison of the basenames. */
1340 if (basenames_may_differ
14bc53a8
PA
1341 || file_matcher (lbasename (ps->filename), true))
1342 match = file_matcher (psymtab_to_fullname (ps), false);
680d1742
DE
1343 }
1344 if (!match)
b4c41fc7
DE
1345 continue;
1346 }
ccefe4c4 1347
c1a66c06
TV
1348 if ((symbol_matcher == NULL && lookup_name == NULL)
1349 || recursively_search_psymtabs (ps, objfile, domain,
1350 lookup_name->make_ignore_params (),
1351 symbol_matcher))
276d885b
GB
1352 {
1353 struct compunit_symtab *symtab =
1354 psymtab_to_symtab (objfile, ps);
1355
1356 if (expansion_notify != NULL)
14bc53a8 1357 expansion_notify (symtab);
276d885b 1358 }
ccefe4c4
TT
1359 }
1360}
1361
95cf5869
DE
1362/* Psymtab version of has_symbols. See its definition in
1363 the definition of quick_symbol_functions in symfile.h. */
83827540 1364
ccefe4c4 1365static int
83827540 1366psym_has_symbols (struct objfile *objfile)
ccefe4c4 1367{
d320c2b5 1368 return objfile->partial_symtabs->psymtabs != NULL;
ccefe4c4
TT
1369}
1370
71a3c369
TT
1371/* Helper function for psym_find_compunit_symtab_by_address that fills
1372 in psymbol_map for a given range of psymbols. */
1373
1374static void
1375psym_fill_psymbol_map (struct objfile *objfile,
1376 struct partial_symtab *psymtab,
1377 std::set<CORE_ADDR> *seen_addrs,
1378 const std::vector<partial_symbol *> &symbols,
1379 int start,
1380 int length)
1381{
1382 for (int i = 0; i < length; ++i)
1383 {
1384 struct partial_symbol *psym = symbols[start + i];
1385
8a6d4234 1386 if (psym->aclass == LOC_STATIC)
71a3c369 1387 {
02e9e7f7 1388 CORE_ADDR addr = psym->address (objfile);
71a3c369
TT
1389 if (seen_addrs->find (addr) == seen_addrs->end ())
1390 {
1391 seen_addrs->insert (addr);
1392 objfile->psymbol_map.emplace_back (addr, psymtab);
1393 }
1394 }
1395 }
1396}
1397
1398/* See find_compunit_symtab_by_address in quick_symbol_functions, in
1399 symfile.h. */
1400
1401static compunit_symtab *
1402psym_find_compunit_symtab_by_address (struct objfile *objfile,
1403 CORE_ADDR address)
1404{
1405 if (objfile->psymbol_map.empty ())
1406 {
71a3c369
TT
1407 std::set<CORE_ADDR> seen_addrs;
1408
26abc753 1409 for (partial_symtab *pst : require_partial_symbols (objfile, true))
b22a7c6a
TT
1410 {
1411 psym_fill_psymbol_map (objfile, pst,
1412 &seen_addrs,
d320c2b5 1413 objfile->partial_symtabs->global_psymbols,
b22a7c6a
TT
1414 pst->globals_offset,
1415 pst->n_global_syms);
1416 psym_fill_psymbol_map (objfile, pst,
1417 &seen_addrs,
d320c2b5 1418 objfile->partial_symtabs->static_psymbols,
b22a7c6a
TT
1419 pst->statics_offset,
1420 pst->n_static_syms);
1421 }
71a3c369
TT
1422
1423 objfile->psymbol_map.shrink_to_fit ();
1424
1425 std::sort (objfile->psymbol_map.begin (), objfile->psymbol_map.end (),
1426 [] (const std::pair<CORE_ADDR, partial_symtab *> &a,
1427 const std::pair<CORE_ADDR, partial_symtab *> &b)
1428 {
1429 return a.first < b.first;
1430 });
1431 }
1432
1433 auto iter = std::lower_bound
1434 (objfile->psymbol_map.begin (), objfile->psymbol_map.end (), address,
1435 [] (const std::pair<CORE_ADDR, partial_symtab *> &a,
1436 CORE_ADDR b)
1437 {
1438 return a.first < b;
1439 });
1440
1441 if (iter == objfile->psymbol_map.end () || iter->first != address)
1442 return NULL;
1443
1444 return psymtab_to_symtab (objfile, iter->second);
1445}
1446
ccefe4c4
TT
1447const struct quick_symbol_functions psym_functions =
1448{
83827540
DE
1449 psym_has_symbols,
1450 psym_find_last_source_symtab,
1451 psym_forget_cached_source_info,
1452 psym_map_symtabs_matching_filename,
1453 psym_lookup_symbol,
d3214198 1454 psym_lookup_global_symbol_language,
83827540
DE
1455 psym_print_stats,
1456 psym_dump,
83827540
DE
1457 psym_expand_symtabs_for_function,
1458 psym_expand_all_symtabs,
1459 psym_expand_symtabs_with_fullname,
1460 psym_map_matching_symbols,
1461 psym_expand_symtabs_matching,
1462 psym_find_pc_sect_compunit_symtab,
71a3c369 1463 psym_find_compunit_symtab_by_address,
83827540 1464 psym_map_symbol_filenames
ccefe4c4
TT
1465};
1466
1467\f
1468
8763cede 1469static void
5c80ed9d 1470sort_pst_symbols (struct objfile *objfile, struct partial_symtab *pst)
ccefe4c4 1471{
0df8b418 1472 /* Sort the global list; don't sort the static list. */
d320c2b5 1473 auto begin = objfile->partial_symtabs->global_psymbols.begin ();
af5bf4ad
SM
1474 std::advance (begin, pst->globals_offset);
1475
1476 /* The psymbols for this partial_symtab are currently at the end of the
1477 vector. */
d320c2b5 1478 auto end = objfile->partial_symtabs->global_psymbols.end ();
ccefe4c4 1479
af5bf4ad
SM
1480 std::sort (begin, end, [] (partial_symbol *s1, partial_symbol *s2)
1481 {
c9d95fa3
CB
1482 return strcmp_iw_ordered (s1->ginfo.search_name (),
1483 s2->ginfo.search_name ()) < 0;
af5bf4ad 1484 });
ccefe4c4
TT
1485}
1486
c3693a1d
TT
1487/* Partially fill a partial symtab. It will be completely filled at
1488 the end of the symbol list. */
ccefe4c4 1489
c3693a1d
TT
1490partial_symtab::partial_symtab (const char *filename,
1491 struct objfile *objfile,
1492 CORE_ADDR textlow)
1493 : partial_symtab (filename, objfile)
ccefe4c4 1494{
c3693a1d
TT
1495 set_text_low (textlow);
1496 set_text_high (raw_text_low ()); /* default */
96c7f873
TV
1497
1498 auto *v1 = new std::vector<partial_symbol *>;
1499 objfile->partial_symtabs->current_global_psymbols.push_back (v1);
1500 auto *v2 = new std::vector<partial_symbol *>;
1501 objfile->partial_symtabs->current_static_psymbols.push_back (v2);
1502}
1503
1504/* Concat vectors V1 and V2. */
1505
1506static void
1507concat (std::vector<partial_symbol *> *v1, std::vector<partial_symbol *> *v2)
1508{
1509 v1->insert (v1->end (), v2->begin (), v2->end ());
1510 v2->clear ();
ccefe4c4
TT
1511}
1512
8763cede
DE
1513/* Perform "finishing up" operations of a partial symtab. */
1514
1515void
1516end_psymtab_common (struct objfile *objfile, struct partial_symtab *pst)
1517{
96c7f873
TV
1518 pst->globals_offset = objfile->partial_symtabs->global_psymbols.size ();
1519 pst->statics_offset = objfile->partial_symtabs->static_psymbols.size ();
1520
1521 auto *current_global_psymbols
1522 = objfile->partial_symtabs->current_global_psymbols.back ();
1523 auto *current_static_psymbols
1524 = objfile->partial_symtabs->current_static_psymbols.back ();
1525 objfile->partial_symtabs->current_global_psymbols.pop_back ();
1526 objfile->partial_symtabs->current_static_psymbols.pop_back ();
1527
1528 pst->n_global_syms
1529 = current_global_psymbols->size ();
1530 pst->n_static_syms
1531 = current_static_psymbols->size ();
1532
1533 concat (&objfile->partial_symtabs->global_psymbols, current_global_psymbols);
1534 concat (&objfile->partial_symtabs->static_psymbols, current_static_psymbols);
1535
1536 delete current_global_psymbols;
1537 delete current_static_psymbols;
8763cede
DE
1538
1539 sort_pst_symbols (objfile, pst);
1540}
1541
cbd70537
SW
1542/* Calculate a hash code for the given partial symbol. The hash is
1543 calculated using the symbol's value, language, domain, class
0df8b418 1544 and name. These are the values which are set by
cbd70537
SW
1545 add_psymbol_to_bcache. */
1546
710e1a31 1547static unsigned long
cbd70537
SW
1548psymbol_hash (const void *addr, int length)
1549{
1550 unsigned long h = 0;
1551 struct partial_symbol *psymbol = (struct partial_symbol *) addr;
c1b5c1eb 1552 unsigned int lang = psymbol->ginfo.language ();
8a6d4234
TT
1553 unsigned int domain = psymbol->domain;
1554 unsigned int theclass = psymbol->aclass;
cbd70537 1555
4cbd39b2
CB
1556 h = fast_hash (&psymbol->ginfo.value, sizeof (psymbol->ginfo.value), h);
1557 h = fast_hash (&lang, sizeof (unsigned int), h);
1558 h = fast_hash (&domain, sizeof (unsigned int), h);
1559 h = fast_hash (&theclass, sizeof (unsigned int), h);
4d4eaa30 1560 /* Note that psymbol names are interned via compute_and_set_names, so
e793c052 1561 there's no need to hash the contents of the name here. */
4d4eaa30 1562 h = fast_hash (&psymbol->ginfo.m_name, sizeof (psymbol->ginfo.m_name), h);
cbd70537
SW
1563
1564 return h;
1565}
1566
1567/* Returns true if the symbol at addr1 equals the symbol at addr2.
1568 For the comparison this function uses a symbols value,
1569 language, domain, class and name. */
1570
710e1a31 1571static int
cbd70537
SW
1572psymbol_compare (const void *addr1, const void *addr2, int length)
1573{
1574 struct partial_symbol *sym1 = (struct partial_symbol *) addr1;
1575 struct partial_symbol *sym2 = (struct partial_symbol *) addr2;
1576
af97b416
TT
1577 return (memcmp (&sym1->ginfo.value, &sym2->ginfo.value,
1578 sizeof (sym1->ginfo.value)) == 0
c1b5c1eb 1579 && sym1->ginfo.language () == sym2->ginfo.language ()
8a6d4234
TT
1580 && sym1->domain == sym2->domain
1581 && sym1->aclass == sym2->aclass
e793c052 1582 /* Note that psymbol names are interned via
4d4eaa30 1583 compute_and_set_names, so there's no need to compare the
e793c052 1584 contents of the name here. */
4d4eaa30 1585 && sym1->ginfo.linkage_name () == sym2->ginfo.linkage_name ());
cbd70537
SW
1586}
1587
95cf5869 1588/* Helper function, initialises partial symbol structure and stashes
ccefe4c4
TT
1589 it into objfile's bcache. Note that our caching mechanism will
1590 use all fields of struct partial_symbol to determine hash value of the
1591 structure. In other words, having two symbols with the same name but
1592 different domain (or address) is possible and correct. */
1593
af5bf4ad 1594static struct partial_symbol *
31edb802 1595add_psymbol_to_bcache (gdb::string_view name, bool copy_name,
ccefe4c4 1596 domain_enum domain,
fe978cb0 1597 enum address_class theclass,
79748972 1598 short section,
1762568f 1599 CORE_ADDR coreaddr,
ccefe4c4
TT
1600 enum language language, struct objfile *objfile,
1601 int *added)
1602{
cbd70537 1603 struct partial_symbol psymbol;
af97b416 1604 memset (&psymbol, 0, sizeof (psymbol));
cbd70537 1605
79748972 1606 psymbol.set_unrelocated_address (coreaddr);
af97b416 1607 psymbol.ginfo.section = section;
8a6d4234
TT
1608 psymbol.domain = domain;
1609 psymbol.aclass = theclass;
d3ecddab 1610 psymbol.ginfo.set_language (language, objfile->partial_symtabs->obstack ());
4d4eaa30 1611 psymbol.ginfo.compute_and_set_names (name, copy_name, objfile->per_bfd);
ccefe4c4 1612
0df8b418 1613 /* Stash the partial symbol away in the cache. */
25629dfd
TT
1614 return ((struct partial_symbol *)
1615 objfile->partial_symtabs->psymbol_cache.insert
1616 (&psymbol, sizeof (struct partial_symbol), added));
ccefe4c4
TT
1617}
1618
95cf5869 1619/* Helper function, adds partial symbol to the given partial symbol list. */
ccefe4c4
TT
1620
1621static void
af5bf4ad
SM
1622append_psymbol_to_list (std::vector<partial_symbol *> *list,
1623 struct partial_symbol *psym,
ccefe4c4
TT
1624 struct objfile *objfile)
1625{
af5bf4ad 1626 list->push_back (psym);
ccefe4c4
TT
1627 OBJSTAT (objfile, n_psyms++);
1628}
1629
703a86c2 1630/* See psympriv.h. */
ccefe4c4 1631
7dc25483 1632void
31edb802 1633add_psymbol_to_list (gdb::string_view name, bool copy_name,
ccefe4c4 1634 domain_enum domain,
fe978cb0 1635 enum address_class theclass,
79748972 1636 short section,
75aedd27 1637 psymbol_placement where,
1762568f 1638 CORE_ADDR coreaddr,
ccefe4c4
TT
1639 enum language language, struct objfile *objfile)
1640{
af5bf4ad 1641 struct partial_symbol *psym;
ccefe4c4
TT
1642
1643 int added;
1644
0df8b418 1645 /* Stash the partial symbol away in the cache. */
31edb802 1646 psym = add_psymbol_to_bcache (name, copy_name, domain, theclass,
79748972 1647 section, coreaddr, language, objfile, &added);
ccefe4c4
TT
1648
1649 /* Do not duplicate global partial symbols. */
75aedd27 1650 if (where == psymbol_placement::GLOBAL && !added)
7dc25483 1651 return;
ccefe4c4 1652
0df8b418 1653 /* Save pointer to partial symbol in psymtab, growing symtab if needed. */
75aedd27
TT
1654 std::vector<partial_symbol *> *list
1655 = (where == psymbol_placement::STATIC
96c7f873
TV
1656 ? objfile->partial_symtabs->current_static_psymbols.back ()
1657 : objfile->partial_symtabs->current_global_psymbols.back ());
ccefe4c4 1658 append_psymbol_to_list (list, psym, objfile);
ccefe4c4
TT
1659}
1660
6eee24ce 1661/* See psympriv.h. */
ccefe4c4
TT
1662
1663void
1664init_psymbol_list (struct objfile *objfile, int total_symbols)
1665{
d320c2b5
TT
1666 if (objfile->partial_symtabs->global_psymbols.capacity () == 0
1667 && objfile->partial_symtabs->static_psymbols.capacity () == 0)
6eee24ce
TT
1668 {
1669 /* Current best guess is that approximately a twentieth of the
1670 total symbols (in a debugging file) are global or static
1671 oriented symbols, then multiply that by slop factor of
1672 two. */
d320c2b5
TT
1673 objfile->partial_symtabs->global_psymbols.reserve (total_symbols / 10);
1674 objfile->partial_symtabs->static_psymbols.reserve (total_symbols / 10);
6eee24ce 1675 }
ccefe4c4
TT
1676}
1677
baa62830
TT
1678/* See psympriv.h. */
1679
32caafd0
TT
1680partial_symtab::partial_symtab (const char *filename_, struct objfile *objfile)
1681 : searched_flag (PST_NOT_SEARCHED),
1682 text_low_valid (0),
1683 text_high_valid (0)
ccefe4c4 1684{
32caafd0 1685 objfile->partial_symtabs->install_psymtab (this);
ccefe4c4 1686
be1e3d3e 1687 filename = objfile->intern (filename_);
ccefe4c4 1688
45cfd468
DE
1689 if (symtab_create_debug)
1690 {
1691 /* Be a bit clever with debugging messages, and don't print objfile
1692 every time, only when it changes. */
1693 static char *last_objfile_name = NULL;
1694
1695 if (last_objfile_name == NULL
4262abfb 1696 || strcmp (last_objfile_name, objfile_name (objfile)) != 0)
45cfd468
DE
1697 {
1698 xfree (last_objfile_name);
4262abfb 1699 last_objfile_name = xstrdup (objfile_name (objfile));
22068491
TT
1700 fprintf_filtered (gdb_stdlog,
1701 "Creating one or more psymtabs for objfile %s ...\n",
1702 last_objfile_name);
45cfd468 1703 }
22068491
TT
1704 fprintf_filtered (gdb_stdlog,
1705 "Created psymtab %s for module %s.\n",
32caafd0 1706 host_address_to_string (this), filename);
45cfd468 1707 }
ccefe4c4
TT
1708}
1709
0494dbec
TT
1710/* See psympriv.h. */
1711
1712void
48993951 1713partial_symtab::expand_dependencies (struct objfile *objfile)
0494dbec
TT
1714{
1715 for (int i = 0; i < number_of_dependencies; ++i)
1716 {
1eb73179
TV
1717 if (!dependencies[i]->readin_p ()
1718 && dependencies[i]->user == NULL)
0494dbec
TT
1719 {
1720 /* Inform about additional files to be read in. */
1721 if (info_verbose)
1722 {
1723 fputs_filtered (" ", gdb_stdout);
1724 wrap_here ("");
1725 fputs_filtered ("and ", gdb_stdout);
1726 wrap_here ("");
1727 printf_filtered ("%s...", dependencies[i]->filename);
1728 wrap_here (""); /* Flush output */
1729 gdb_flush (gdb_stdout);
1730 }
1731 dependencies[i]->expand_psymtab (objfile);
1732 }
1733 }
1734}
1735
1736
ccefe4c4 1737void
d320c2b5 1738psymtab_storage::discard_psymtab (struct partial_symtab *pst)
ccefe4c4
TT
1739{
1740 struct partial_symtab **prev_pst;
1741
1742 /* From dbxread.c:
1743 Empty psymtabs happen as a result of header files which don't
1744 have any symbols in them. There can be a lot of them. But this
1745 check is wrong, in that a psymtab with N_SLINE entries but
1746 nothing else is not empty, but we don't realize that. Fixing
1747 that without slowing things down might be tricky. */
1748
0df8b418 1749 /* First, snip it out of the psymtab chain. */
ccefe4c4 1750
d320c2b5 1751 prev_pst = &psymtabs;
ccefe4c4
TT
1752 while ((*prev_pst) != pst)
1753 prev_pst = &((*prev_pst)->next);
1754 (*prev_pst) = pst->next;
6f17252b 1755 delete pst;
ccefe4c4
TT
1756}
1757
ccefe4c4
TT
1758\f
1759
372405a5
DE
1760/* We need to pass a couple of items to the addrmap_foreach function,
1761 so use a struct. */
1762
1763struct dump_psymtab_addrmap_data
1764{
1765 struct objfile *objfile;
1766 struct partial_symtab *psymtab;
1767 struct ui_file *outfile;
1768
1769 /* Non-zero if the previously printed addrmap entry was for PSYMTAB.
1770 If so, we want to print the next one as well (since the next addrmap
1771 entry defines the end of the range). */
1772 int previous_matched;
1773};
1774
1775/* Helper function for dump_psymtab_addrmap to print an addrmap entry. */
1776
1777static int
1778dump_psymtab_addrmap_1 (void *datap, CORE_ADDR start_addr, void *obj)
1779{
709b5518
PA
1780 struct dump_psymtab_addrmap_data *data
1781 = (struct dump_psymtab_addrmap_data *) datap;
08feed99 1782 struct gdbarch *gdbarch = data->objfile->arch ();
709b5518 1783 struct partial_symtab *addrmap_psymtab = (struct partial_symtab *) obj;
372405a5
DE
1784 const char *psymtab_address_or_end = NULL;
1785
1786 QUIT;
1787
1788 if (data->psymtab == NULL
1789 || data->psymtab == addrmap_psymtab)
1790 psymtab_address_or_end = host_address_to_string (addrmap_psymtab);
1791 else if (data->previous_matched)
1792 psymtab_address_or_end = "<ends here>";
1793
1794 if (data->psymtab == NULL
1795 || data->psymtab == addrmap_psymtab
1796 || data->previous_matched)
1797 {
1798 fprintf_filtered (data->outfile, " %s%s %s\n",
1799 data->psymtab != NULL ? " " : "",
1800 paddress (gdbarch, start_addr),
1801 psymtab_address_or_end);
1802 }
1803
1804 data->previous_matched = (data->psymtab == NULL
1805 || data->psymtab == addrmap_psymtab);
1806
1807 return 0;
1808}
1809
1810/* Helper function for maintenance_print_psymbols to print the addrmap
1811 of PSYMTAB. If PSYMTAB is NULL print the entire addrmap. */
1812
1813static void
1814dump_psymtab_addrmap (struct objfile *objfile, struct partial_symtab *psymtab,
1815 struct ui_file *outfile)
1816{
1817 struct dump_psymtab_addrmap_data addrmap_dump_data;
1818
34c41c68
DE
1819 if ((psymtab == NULL
1820 || psymtab->psymtabs_addrmap_supported)
d320c2b5 1821 && objfile->partial_symtabs->psymtabs_addrmap != NULL)
372405a5
DE
1822 {
1823 addrmap_dump_data.objfile = objfile;
1824 addrmap_dump_data.psymtab = psymtab;
1825 addrmap_dump_data.outfile = outfile;
1826 addrmap_dump_data.previous_matched = 0;
1827 fprintf_filtered (outfile, "%sddress map:\n",
1828 psymtab == NULL ? "Entire a" : " A");
d320c2b5
TT
1829 addrmap_foreach (objfile->partial_symtabs->psymtabs_addrmap,
1830 dump_psymtab_addrmap_1, &addrmap_dump_data);
372405a5
DE
1831 }
1832}
1833
dfc7bb5b 1834static void
990b9f9f 1835maintenance_print_psymbols (const char *args, int from_tty)
ccefe4c4 1836{
34c41c68 1837 struct ui_file *outfile = gdb_stdout;
34c41c68 1838 char *address_arg = NULL, *source_arg = NULL, *objfile_arg = NULL;
34c41c68
DE
1839 int i, outfile_idx, found;
1840 CORE_ADDR pc = 0;
1841 struct obj_section *section = NULL;
ccefe4c4
TT
1842
1843 dont_repeat ();
1844
773a1edc 1845 gdb_argv argv (args);
ccefe4c4 1846
99e8a4f9 1847 for (i = 0; argv != NULL && argv[i] != NULL; ++i)
ccefe4c4 1848 {
34c41c68
DE
1849 if (strcmp (argv[i], "-pc") == 0)
1850 {
1851 if (argv[i + 1] == NULL)
1852 error (_("Missing pc value"));
1853 address_arg = argv[++i];
1854 }
1855 else if (strcmp (argv[i], "-source") == 0)
1856 {
1857 if (argv[i + 1] == NULL)
1858 error (_("Missing source file"));
1859 source_arg = argv[++i];
1860 }
1861 else if (strcmp (argv[i], "-objfile") == 0)
1862 {
1863 if (argv[i + 1] == NULL)
1864 error (_("Missing objfile name"));
1865 objfile_arg = argv[++i];
1866 }
1867 else if (strcmp (argv[i], "--") == 0)
1868 {
1869 /* End of options. */
1870 ++i;
1871 break;
1872 }
1873 else if (argv[i][0] == '-')
ccefe4c4 1874 {
34c41c68
DE
1875 /* Future proofing: Don't allow OUTFILE to begin with "-". */
1876 error (_("Unknown option: %s"), argv[i]);
ccefe4c4 1877 }
34c41c68
DE
1878 else
1879 break;
ccefe4c4 1880 }
34c41c68 1881 outfile_idx = i;
ccefe4c4 1882
34c41c68
DE
1883 if (address_arg != NULL && source_arg != NULL)
1884 error (_("Must specify at most one of -pc and -source"));
ccefe4c4 1885
d7e74731
PA
1886 stdio_file arg_outfile;
1887
99e8a4f9 1888 if (argv != NULL && argv[outfile_idx] != NULL)
34c41c68 1889 {
34c41c68
DE
1890 if (argv[outfile_idx + 1] != NULL)
1891 error (_("Junk at end of command"));
ee0c3293
TT
1892 gdb::unique_xmalloc_ptr<char> outfile_name
1893 (tilde_expand (argv[outfile_idx]));
1894 if (!arg_outfile.open (outfile_name.get (), FOPEN_WT))
1895 perror_with_name (outfile_name.get ());
d7e74731 1896 outfile = &arg_outfile;
34c41c68 1897 }
ccefe4c4 1898
34c41c68
DE
1899 if (address_arg != NULL)
1900 {
1901 pc = parse_and_eval_address (address_arg);
1902 /* If we fail to find a section, that's ok, try the lookup anyway. */
1903 section = find_pc_section (pc);
1904 }
372405a5 1905
34c41c68 1906 found = 0;
2030c079 1907 for (objfile *objfile : current_program_space->objfiles ())
27618ce4 1908 {
34c41c68
DE
1909 int printed_objfile_header = 0;
1910 int print_for_objfile = 1;
1911
27618ce4 1912 QUIT;
34c41c68
DE
1913 if (objfile_arg != NULL)
1914 print_for_objfile
1915 = compare_filenames_for_search (objfile_name (objfile),
1916 objfile_arg);
1917 if (!print_for_objfile)
1918 continue;
1919
1920 if (address_arg != NULL)
1921 {
1922 struct bound_minimal_symbol msymbol = { NULL, NULL };
1923
1924 /* We don't assume each pc has a unique objfile (this is for
1925 debugging). */
b22a7c6a
TT
1926 struct partial_symtab *ps = find_pc_sect_psymtab (objfile, pc,
1927 section, msymbol);
34c41c68
DE
1928 if (ps != NULL)
1929 {
1930 if (!printed_objfile_header)
1931 {
d7e74731
PA
1932 outfile->printf ("\nPartial symtabs for objfile %s\n",
1933 objfile_name (objfile));
34c41c68
DE
1934 printed_objfile_header = 1;
1935 }
1936 dump_psymtab (objfile, ps, outfile);
1937 dump_psymtab_addrmap (objfile, ps, outfile);
1938 found = 1;
1939 }
1940 }
1941 else
1942 {
26abc753 1943 for (partial_symtab *ps : require_partial_symbols (objfile, true))
34c41c68
DE
1944 {
1945 int print_for_source = 0;
1946
1947 QUIT;
1948 if (source_arg != NULL)
1949 {
1950 print_for_source
1951 = compare_filenames_for_search (ps->filename, source_arg);
1952 found = 1;
1953 }
1954 if (source_arg == NULL
1955 || print_for_source)
1956 {
1957 if (!printed_objfile_header)
1958 {
d7e74731
PA
1959 outfile->printf ("\nPartial symtabs for objfile %s\n",
1960 objfile_name (objfile));
34c41c68
DE
1961 printed_objfile_header = 1;
1962 }
1963 dump_psymtab (objfile, ps, outfile);
1964 dump_psymtab_addrmap (objfile, ps, outfile);
1965 }
1966 }
1967 }
1968
1969 /* If we're printing all the objfile's symbols dump the full addrmap. */
1970
1971 if (address_arg == NULL
1972 && source_arg == NULL
d320c2b5 1973 && objfile->partial_symtabs->psymtabs_addrmap != NULL)
372405a5 1974 {
d7e74731 1975 outfile->puts ("\n");
34c41c68 1976 dump_psymtab_addrmap (objfile, NULL, outfile);
372405a5 1977 }
27618ce4 1978 }
372405a5 1979
34c41c68
DE
1980 if (!found)
1981 {
1982 if (address_arg != NULL)
1983 error (_("No partial symtab for address: %s"), address_arg);
1984 if (source_arg != NULL)
1985 error (_("No partial symtab for source file: %s"), source_arg);
1986 }
ccefe4c4
TT
1987}
1988
1989/* List all the partial symbol tables whose names match REGEXP (optional). */
95cf5869 1990
dfc7bb5b 1991static void
990b9f9f 1992maintenance_info_psymtabs (const char *regexp, int from_tty)
ccefe4c4
TT
1993{
1994 struct program_space *pspace;
ccefe4c4
TT
1995
1996 if (regexp)
1997 re_comp (regexp);
1998
1999 ALL_PSPACES (pspace)
2030c079 2000 for (objfile *objfile : pspace->objfiles ())
99d89cde 2001 {
08feed99 2002 struct gdbarch *gdbarch = objfile->arch ();
ccefe4c4 2003
99d89cde
TT
2004 /* We don't want to print anything for this objfile until we
2005 actually find a symtab whose name matches. */
2006 int printed_objfile_start = 0;
ccefe4c4 2007
26abc753 2008 for (partial_symtab *psymtab : require_partial_symbols (objfile, true))
99d89cde
TT
2009 {
2010 QUIT;
2011
2012 if (! regexp
2013 || re_exec (psymtab->filename))
2014 {
2015 if (! printed_objfile_start)
2016 {
2017 printf_filtered ("{ objfile %s ", objfile_name (objfile));
2018 wrap_here (" ");
2019 printf_filtered ("((struct objfile *) %s)\n",
2020 host_address_to_string (objfile));
2021 printed_objfile_start = 1;
2022 }
2023
2024 printf_filtered (" { psymtab %s ", psymtab->filename);
2025 wrap_here (" ");
2026 printf_filtered ("((struct partial_symtab *) %s)\n",
2027 host_address_to_string (psymtab));
2028
2029 printf_filtered (" readin %s\n",
128a391f 2030 psymtab->readin_p () ? "yes" : "no");
99d89cde
TT
2031 printf_filtered (" fullname %s\n",
2032 psymtab->fullname
2033 ? psymtab->fullname : "(null)");
2034 printf_filtered (" text addresses ");
2035 fputs_filtered (paddress (gdbarch,
2036 psymtab->text_low (objfile)),
2037 gdb_stdout);
2038 printf_filtered (" -- ");
2039 fputs_filtered (paddress (gdbarch,
2040 psymtab->text_high (objfile)),
2041 gdb_stdout);
2042 printf_filtered ("\n");
2043 printf_filtered (" psymtabs_addrmap_supported %s\n",
2044 (psymtab->psymtabs_addrmap_supported
2045 ? "yes" : "no"));
2046 printf_filtered (" globals ");
2047 if (psymtab->n_global_syms)
2048 {
d320c2b5
TT
2049 auto p = &(objfile->partial_symtabs
2050 ->global_psymbols[psymtab->globals_offset]);
99d89cde
TT
2051
2052 printf_filtered
2053 ("(* (struct partial_symbol **) %s @ %d)\n",
2054 host_address_to_string (p),
2055 psymtab->n_global_syms);
2056 }
2057 else
2058 printf_filtered ("(none)\n");
2059 printf_filtered (" statics ");
2060 if (psymtab->n_static_syms)
2061 {
d320c2b5
TT
2062 auto p = &(objfile->partial_symtabs
2063 ->static_psymbols[psymtab->statics_offset]);
99d89cde
TT
2064
2065 printf_filtered
2066 ("(* (struct partial_symbol **) %s @ %d)\n",
2067 host_address_to_string (p),
2068 psymtab->n_static_syms);
2069 }
2070 else
2071 printf_filtered ("(none)\n");
a64fafb5
TV
2072 if (psymtab->user)
2073 printf_filtered (" user %s "
2074 "((struct partial_symtab *) %s)\n",
2075 psymtab->user->filename,
2076 host_address_to_string (psymtab->user));
99d89cde
TT
2077 printf_filtered (" dependencies ");
2078 if (psymtab->number_of_dependencies)
2079 {
2080 int i;
2081
2082 printf_filtered ("{\n");
2083 for (i = 0; i < psymtab->number_of_dependencies; i++)
2084 {
2085 struct partial_symtab *dep = psymtab->dependencies[i];
2086
2087 /* Note the string concatenation there --- no
2088 comma. */
2089 printf_filtered (" psymtab %s "
2090 "((struct partial_symtab *) %s)\n",
2091 dep->filename,
2092 host_address_to_string (dep));
2093 }
2094 printf_filtered (" }\n");
2095 }
2096 else
2097 printf_filtered ("(none)\n");
2098 printf_filtered (" }\n");
2099 }
2100 }
ccefe4c4 2101
99d89cde
TT
2102 if (printed_objfile_start)
2103 printf_filtered ("}\n");
2104 }
ccefe4c4
TT
2105}
2106
7d0c9981 2107/* Check consistency of currently expanded psymtabs vs symtabs. */
ccefe4c4 2108
dfc7bb5b 2109static void
990b9f9f 2110maintenance_check_psymtabs (const char *ignore, int from_tty)
ccefe4c4
TT
2111{
2112 struct symbol *sym;
43f3e411 2113 struct compunit_symtab *cust = NULL;
346d1dfe 2114 const struct blockvector *bv;
582942f4 2115 const struct block *b;
5707e24b 2116 int i;
ccefe4c4 2117
2030c079 2118 for (objfile *objfile : current_program_space->objfiles ())
26abc753 2119 for (partial_symtab *ps : require_partial_symbols (objfile, true))
7d0c9981 2120 {
08feed99 2121 struct gdbarch *gdbarch = objfile->arch ();
7d0c9981 2122
aed57c53
TT
2123 /* We don't call psymtab_to_symtab here because that may cause symtab
2124 expansion. When debugging a problem it helps if checkers leave
2125 things unchanged. */
128a391f 2126 cust = ps->get_compunit_symtab ();
aed57c53
TT
2127
2128 /* First do some checks that don't require the associated symtab. */
2129 if (ps->text_high (objfile) < ps->text_low (objfile))
ccefe4c4 2130 {
aed57c53 2131 printf_filtered ("Psymtab ");
ccefe4c4 2132 puts_filtered (ps->filename);
aed57c53
TT
2133 printf_filtered (" covers bad range ");
2134 fputs_filtered (paddress (gdbarch, ps->text_low (objfile)),
2135 gdb_stdout);
2136 printf_filtered (" - ");
2137 fputs_filtered (paddress (gdbarch, ps->text_high (objfile)),
2138 gdb_stdout);
2139 printf_filtered ("\n");
2140 continue;
ccefe4c4 2141 }
aed57c53
TT
2142
2143 /* Now do checks requiring the associated symtab. */
2144 if (cust == NULL)
2145 continue;
2146 bv = COMPUNIT_BLOCKVECTOR (cust);
2147 b = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
d320c2b5
TT
2148 partial_symbol **psym
2149 = &objfile->partial_symtabs->static_psymbols[ps->statics_offset];
5707e24b 2150 for (i = 0; i < ps->n_static_syms; psym++, i++)
aed57c53 2151 {
5707e24b
TV
2152 /* Skip symbols for inlined functions without address. These may
2153 or may not have a match in the full symtab. */
2154 if ((*psym)->aclass == LOC_BLOCK
2155 && (*psym)->ginfo.value.address == 0)
2156 continue;
2157
c9d95fa3 2158 sym = block_lookup_symbol (b, (*psym)->ginfo.search_name (),
aed57c53
TT
2159 symbol_name_match_type::SEARCH_NAME,
2160 (*psym)->domain);
2161 if (!sym)
2162 {
2163 printf_filtered ("Static symbol `");
4d4eaa30 2164 puts_filtered ((*psym)->ginfo.linkage_name ());
aed57c53
TT
2165 printf_filtered ("' only found in ");
2166 puts_filtered (ps->filename);
2167 printf_filtered (" psymtab\n");
2168 }
aed57c53
TT
2169 }
2170 b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
d320c2b5 2171 psym = &objfile->partial_symtabs->global_psymbols[ps->globals_offset];
5707e24b 2172 for (i = 0; i < ps->n_global_syms; psym++, i++)
ccefe4c4 2173 {
c9d95fa3 2174 sym = block_lookup_symbol (b, (*psym)->ginfo.search_name (),
aed57c53
TT
2175 symbol_name_match_type::SEARCH_NAME,
2176 (*psym)->domain);
2177 if (!sym)
2178 {
2179 printf_filtered ("Global symbol `");
4d4eaa30 2180 puts_filtered ((*psym)->ginfo.linkage_name ());
aed57c53
TT
2181 printf_filtered ("' only found in ");
2182 puts_filtered (ps->filename);
2183 printf_filtered (" psymtab\n");
2184 }
aed57c53
TT
2185 }
2186 if (ps->raw_text_high () != 0
2187 && (ps->text_low (objfile) < BLOCK_START (b)
2188 || ps->text_high (objfile) > BLOCK_END (b)))
2189 {
2190 printf_filtered ("Psymtab ");
ccefe4c4 2191 puts_filtered (ps->filename);
aed57c53
TT
2192 printf_filtered (" covers ");
2193 fputs_filtered (paddress (gdbarch, ps->text_low (objfile)),
2194 gdb_stdout);
2195 printf_filtered (" - ");
2196 fputs_filtered (paddress (gdbarch, ps->text_high (objfile)),
2197 gdb_stdout);
2198 printf_filtered (" but symtab covers only ");
2199 fputs_filtered (paddress (gdbarch, BLOCK_START (b)), gdb_stdout);
2200 printf_filtered (" - ");
2201 fputs_filtered (paddress (gdbarch, BLOCK_END (b)), gdb_stdout);
2202 printf_filtered ("\n");
ccefe4c4 2203 }
ccefe4c4 2204 }
ccefe4c4
TT
2205}
2206
6c265988 2207void _initialize_psymtab ();
dfc7bb5b 2208void
6c265988 2209_initialize_psymtab ()
dfc7bb5b
YQ
2210{
2211 add_cmd ("psymbols", class_maintenance, maintenance_print_psymbols, _("\
2212Print dump of current partial symbol definitions.\n\
48c5e7e2
TT
2213Usage: mt print psymbols [-objfile OBJFILE] [-pc ADDRESS] [--] [OUTFILE]\n\
2214 mt print psymbols [-objfile OBJFILE] [-source SOURCE] [--] [OUTFILE]\n\
34c41c68
DE
2215Entries in the partial symbol table are dumped to file OUTFILE,\n\
2216or the terminal if OUTFILE is unspecified.\n\
2217If ADDRESS is provided, dump only the file for that address.\n\
2218If SOURCE is provided, dump only that file's symbols.\n\
2219If OBJFILE is provided, dump only that file's minimal symbols."),
dfc7bb5b
YQ
2220 &maintenanceprintlist);
2221
2222 add_cmd ("psymtabs", class_maintenance, maintenance_info_psymtabs, _("\
2223List the partial symbol tables for all object files.\n\
2224This does not include information about individual partial symbols,\n\
2225just the symbol table structures themselves."),
2226 &maintenanceinfolist);
2227
7d0c9981
DE
2228 add_cmd ("check-psymtabs", class_maintenance, maintenance_check_psymtabs,
2229 _("\
2230Check consistency of currently expanded psymtabs versus symtabs."),
dfc7bb5b
YQ
2231 &maintenancelist);
2232}
This page took 1.504274 seconds and 4 git commands to generate.