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