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