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