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