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