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