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