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