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