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