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