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