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