1 /* Support routines for building symbol tables in GDB's internal format.
2 Copyright 1986-2000 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* This module provides subroutines used for creating and adding to
22 the symbol table. These routines are called from various symbol-
23 file-reading routines.
25 Routines to support specific debugging information formats (stabs,
26 DWARF, etc) belong somewhere else. */
32 #include "symfile.h" /* Needed for "struct complaint" */
35 #include "complaints.h"
36 #include "gdb_string.h"
37 #include "language.h" /* For "longest_local_hex_string_custom" */
39 /* Ask buildsym.h to define the vars it normally declares `extern'. */
42 #include "buildsym.h" /* Our own declarations */
45 /* For cleanup_undefined_types and finish_global_stabs (somewhat
46 questionable--see comment where we call them). */
48 #include "stabsread.h"
50 /* List of free `struct pending' structures for reuse. */
52 static struct pending
*free_pendings
;
54 /* Non-zero if symtab has line number info. This prevents an
55 otherwise empty symtab from being tossed. */
57 static int have_line_numbers
;
59 static int compare_line_numbers (const void *ln1p
, const void *ln2p
);
62 /* Initial sizes of data structures. These are realloc'd larger if
63 needed, and realloc'd down to the size actually used, when
66 #define INITIAL_CONTEXT_STACK_SIZE 10
67 #define INITIAL_LINE_VECTOR_LENGTH 1000
70 /* Complaints about the symbols we have encountered. */
72 struct complaint block_end_complaint
=
73 {"block end address less than block start address in %s (patched it)", 0, 0};
75 struct complaint anon_block_end_complaint
=
76 {"block end address 0x%lx less than block start address 0x%lx (patched it)", 0, 0};
78 struct complaint innerblock_complaint
=
79 {"inner block not inside outer block in %s", 0, 0};
81 struct complaint innerblock_anon_complaint
=
82 {"inner block (0x%lx-0x%lx) not inside outer block (0x%lx-0x%lx)", 0, 0};
84 struct complaint blockvector_complaint
=
85 {"block at %s out of order", 0, 0};
87 /* maintain the lists of symbols and blocks */
89 /* Add a pending list to free_pendings. */
91 add_free_pendings (struct pending
*list
)
93 register struct pending
*link
= list
;
97 while (link
->next
) link
= link
->next
;
98 link
->next
= free_pendings
;
103 /* Add a symbol to one of the lists of symbols. */
106 add_symbol_to_list (struct symbol
*symbol
, struct pending
**listhead
)
108 register struct pending
*link
;
110 /* If this is an alias for another symbol, don't add it. */
111 if (symbol
->ginfo
.name
&& symbol
->ginfo
.name
[0] == '#')
114 /* We keep PENDINGSIZE symbols in each link of the list. If we
115 don't have a link with room in it, add a new link. */
116 if (*listhead
== NULL
|| (*listhead
)->nsyms
== PENDINGSIZE
)
120 link
= free_pendings
;
121 free_pendings
= link
->next
;
125 link
= (struct pending
*) xmalloc (sizeof (struct pending
));
128 link
->next
= *listhead
;
133 (*listhead
)->symbol
[(*listhead
)->nsyms
++] = symbol
;
136 /* Find a symbol named NAME on a LIST. NAME need not be
137 '\0'-terminated; LENGTH is the length of the name. */
140 find_symbol_in_list (struct pending
*list
, char *name
, int length
)
147 for (j
= list
->nsyms
; --j
>= 0;)
149 pp
= SYMBOL_NAME (list
->symbol
[j
]);
150 if (*pp
== *name
&& strncmp (pp
, name
, length
) == 0 &&
153 return (list
->symbol
[j
]);
161 /* At end of reading syms, or in case of quit, really free as many
162 `struct pending's as we can easily find. */
166 really_free_pendings (PTR dummy
)
168 struct pending
*next
, *next1
;
170 for (next
= free_pendings
; next
; next
= next1
)
173 free ((void *) next
);
175 free_pendings
= NULL
;
177 free_pending_blocks ();
179 for (next
= file_symbols
; next
!= NULL
; next
= next1
)
182 free ((void *) next
);
186 for (next
= global_symbols
; next
!= NULL
; next
= next1
)
189 free ((void *) next
);
191 global_symbols
= NULL
;
194 /* This function is called to discard any pending blocks. */
197 free_pending_blocks (void)
199 #if 0 /* Now we make the links in the
200 symbol_obstack, so don't free
202 struct pending_block
*bnext
, *bnext1
;
204 for (bnext
= pending_blocks
; bnext
; bnext
= bnext1
)
206 bnext1
= bnext
->next
;
207 free ((void *) bnext
);
210 pending_blocks
= NULL
;
213 /* Take one of the lists of symbols and make a block from it. Keep
214 the order the symbols have in the list (reversed from the input
215 file). Put the block on the list of pending blocks. */
218 finish_block (struct symbol
*symbol
, struct pending
**listhead
,
219 struct pending_block
*old_blocks
,
220 CORE_ADDR start
, CORE_ADDR end
,
221 struct objfile
*objfile
)
223 register struct pending
*next
, *next1
;
224 register struct block
*block
;
225 register struct pending_block
*pblock
;
226 struct pending_block
*opblock
;
230 /* Count the length of the list of symbols. */
232 for (next
= *listhead
, i
= 0;
234 i
+= next
->nsyms
, next
= next
->next
)
239 block
= (struct block
*) obstack_alloc (&objfile
->symbol_obstack
,
240 (sizeof (struct block
) + ((i
- 1) * sizeof (struct symbol
*))));
242 /* Copy the symbols into the block. */
244 BLOCK_NSYMS (block
) = i
;
245 for (next
= *listhead
; next
; next
= next
->next
)
247 for (j
= next
->nsyms
- 1; j
>= 0; j
--)
249 BLOCK_SYM (block
, --i
) = next
->symbol
[j
];
253 BLOCK_START (block
) = start
;
254 BLOCK_END (block
) = end
;
255 /* Superblock filled in when containing block is made */
256 BLOCK_SUPERBLOCK (block
) = NULL
;
258 BLOCK_GCC_COMPILED (block
) = processing_gcc_compilation
;
260 /* Put the block in as the value of the symbol that names it. */
264 struct type
*ftype
= SYMBOL_TYPE (symbol
);
265 SYMBOL_BLOCK_VALUE (symbol
) = block
;
266 BLOCK_FUNCTION (block
) = symbol
;
268 if (TYPE_NFIELDS (ftype
) <= 0)
270 /* No parameter type information is recorded with the
271 function's type. Set that from the type of the
272 parameter symbols. */
273 int nparams
= 0, iparams
;
275 for (i
= 0; i
< BLOCK_NSYMS (block
); i
++)
277 sym
= BLOCK_SYM (block
, i
);
278 switch (SYMBOL_CLASS (sym
))
283 case LOC_REGPARM_ADDR
:
284 case LOC_BASEREG_ARG
:
297 case LOC_CONST_BYTES
:
300 case LOC_OPTIMIZED_OUT
:
307 TYPE_NFIELDS (ftype
) = nparams
;
308 TYPE_FIELDS (ftype
) = (struct field
*)
309 TYPE_ALLOC (ftype
, nparams
* sizeof (struct field
));
311 for (i
= iparams
= 0; iparams
< nparams
; i
++)
313 sym
= BLOCK_SYM (block
, i
);
314 switch (SYMBOL_CLASS (sym
))
319 case LOC_REGPARM_ADDR
:
320 case LOC_BASEREG_ARG
:
322 TYPE_FIELD_TYPE (ftype
, iparams
) = SYMBOL_TYPE (sym
);
334 case LOC_CONST_BYTES
:
337 case LOC_OPTIMIZED_OUT
:
347 BLOCK_FUNCTION (block
) = NULL
;
350 /* Now "free" the links of the list, and empty the list. */
352 for (next
= *listhead
; next
; next
= next1
)
355 next
->next
= free_pendings
;
356 free_pendings
= next
;
361 /* Check to be sure that the blocks have an end address that is
362 greater than starting address */
364 if (BLOCK_END (block
) < BLOCK_START (block
))
368 complain (&block_end_complaint
, SYMBOL_SOURCE_NAME (symbol
));
372 complain (&anon_block_end_complaint
, BLOCK_END (block
), BLOCK_START (block
));
374 /* Better than nothing */
375 BLOCK_END (block
) = BLOCK_START (block
);
379 /* Install this block as the superblock of all blocks made since the
380 start of this scope that don't have superblocks yet. */
383 for (pblock
= pending_blocks
; pblock
!= old_blocks
; pblock
= pblock
->next
)
385 if (BLOCK_SUPERBLOCK (pblock
->block
) == NULL
)
388 /* Check to be sure the blocks are nested as we receive
389 them. If the compiler/assembler/linker work, this just
390 burns a small amount of time. */
391 if (BLOCK_START (pblock
->block
) < BLOCK_START (block
) ||
392 BLOCK_END (pblock
->block
) > BLOCK_END (block
))
396 complain (&innerblock_complaint
,
397 SYMBOL_SOURCE_NAME (symbol
));
401 complain (&innerblock_anon_complaint
, BLOCK_START (pblock
->block
),
402 BLOCK_END (pblock
->block
), BLOCK_START (block
),
405 if (BLOCK_START (pblock
->block
) < BLOCK_START (block
))
406 BLOCK_START (pblock
->block
) = BLOCK_START (block
);
407 if (BLOCK_END (pblock
->block
) > BLOCK_END (block
))
408 BLOCK_END (pblock
->block
) = BLOCK_END (block
);
411 BLOCK_SUPERBLOCK (pblock
->block
) = block
;
416 record_pending_block (objfile
, block
, opblock
);
419 /* Record BLOCK on the list of all blocks in the file. Put it after
420 OPBLOCK, or at the beginning if opblock is NULL. This puts the
421 block in the list after all its subblocks.
423 Allocate the pending block struct in the symbol_obstack to save
424 time. This wastes a little space. FIXME: Is it worth it? */
427 record_pending_block (struct objfile
*objfile
, struct block
*block
,
428 struct pending_block
*opblock
)
430 register struct pending_block
*pblock
;
432 pblock
= (struct pending_block
*)
433 obstack_alloc (&objfile
->symbol_obstack
, sizeof (struct pending_block
));
434 pblock
->block
= block
;
437 pblock
->next
= opblock
->next
;
438 opblock
->next
= pblock
;
442 pblock
->next
= pending_blocks
;
443 pending_blocks
= pblock
;
447 /* Note that this is only used in this file and in dstread.c, which
448 should be fixed to not need direct access to this function. When
449 that is done, it can be made static again. */
452 make_blockvector (struct objfile
*objfile
)
454 register struct pending_block
*next
;
455 register struct blockvector
*blockvector
;
458 /* Count the length of the list of blocks. */
460 for (next
= pending_blocks
, i
= 0; next
; next
= next
->next
, i
++)
464 blockvector
= (struct blockvector
*)
465 obstack_alloc (&objfile
->symbol_obstack
,
466 (sizeof (struct blockvector
)
467 + (i
- 1) * sizeof (struct block
*)));
469 /* Copy the blocks into the blockvector. This is done in reverse
470 order, which happens to put the blocks into the proper order
471 (ascending starting address). finish_block has hair to insert
472 each block into the list after its subblocks in order to make
473 sure this is true. */
475 BLOCKVECTOR_NBLOCKS (blockvector
) = i
;
476 for (next
= pending_blocks
; next
; next
= next
->next
)
478 BLOCKVECTOR_BLOCK (blockvector
, --i
) = next
->block
;
481 #if 0 /* Now we make the links in the
482 obstack, so don't free them. */
483 /* Now free the links of the list, and empty the list. */
485 for (next
= pending_blocks
; next
; next
= next1
)
491 pending_blocks
= NULL
;
493 #if 1 /* FIXME, shut this off after a while
494 to speed up symbol reading. */
495 /* Some compilers output blocks in the wrong order, but we depend on
496 their being in the right order so we can binary search. Check the
497 order and moan about it. FIXME. */
498 if (BLOCKVECTOR_NBLOCKS (blockvector
) > 1)
500 for (i
= 1; i
< BLOCKVECTOR_NBLOCKS (blockvector
); i
++)
502 if (BLOCK_START (BLOCKVECTOR_BLOCK (blockvector
, i
- 1))
503 > BLOCK_START (BLOCKVECTOR_BLOCK (blockvector
, i
)))
506 = BLOCK_START (BLOCKVECTOR_BLOCK (blockvector
, i
));
508 complain (&blockvector_complaint
,
509 longest_local_hex_string ((LONGEST
) start
));
515 return (blockvector
);
518 /* Start recording information about source code that came from an
519 included (or otherwise merged-in) source file with a different
520 name. NAME is the name of the file (cannot be NULL), DIRNAME is
521 the directory in which it resides (or NULL if not known). */
524 start_subfile (char *name
, char *dirname
)
526 register struct subfile
*subfile
;
528 /* See if this subfile is already known as a subfile of the current
531 for (subfile
= subfiles
; subfile
; subfile
= subfile
->next
)
533 if (STREQ (subfile
->name
, name
))
535 current_subfile
= subfile
;
540 /* This subfile is not known. Add an entry for it. Make an entry
541 for this subfile in the list of all subfiles of the current main
544 subfile
= (struct subfile
*) xmalloc (sizeof (struct subfile
));
545 memset ((char *) subfile
, 0, sizeof (struct subfile
));
546 subfile
->next
= subfiles
;
548 current_subfile
= subfile
;
550 /* Save its name and compilation directory name */
551 subfile
->name
= (name
== NULL
) ? NULL
: savestring (name
, strlen (name
));
553 (dirname
== NULL
) ? NULL
: savestring (dirname
, strlen (dirname
));
555 /* Initialize line-number recording for this subfile. */
556 subfile
->line_vector
= NULL
;
558 /* Default the source language to whatever can be deduced from the
559 filename. If nothing can be deduced (such as for a C/C++ include
560 file with a ".h" extension), then inherit whatever language the
561 previous subfile had. This kludgery is necessary because there
562 is no standard way in some object formats to record the source
563 language. Also, when symtabs are allocated we try to deduce a
564 language then as well, but it is too late for us to use that
565 information while reading symbols, since symtabs aren't allocated
566 until after all the symbols have been processed for a given
569 subfile
->language
= deduce_language_from_filename (subfile
->name
);
570 if (subfile
->language
== language_unknown
&&
571 subfile
->next
!= NULL
)
573 subfile
->language
= subfile
->next
->language
;
576 /* Initialize the debug format string to NULL. We may supply it
577 later via a call to record_debugformat. */
578 subfile
->debugformat
= NULL
;
580 /* cfront output is a C program, so in most ways it looks like a C
581 program. But to demangle we need to set the language to C++. We
582 can distinguish cfront code by the fact that it has #line
583 directives which specify a file name ending in .C.
585 So if the filename of this subfile ends in .C, then change the
586 language of any pending subfiles from C to C++. We also accept
587 any other C++ suffixes accepted by deduce_language_from_filename
588 (in particular, some people use .cxx with cfront). */
589 /* Likewise for f2c. */
594 enum language sublang
= deduce_language_from_filename (subfile
->name
);
596 if (sublang
== language_cplus
|| sublang
== language_fortran
)
597 for (s
= subfiles
; s
!= NULL
; s
= s
->next
)
598 if (s
->language
== language_c
)
599 s
->language
= sublang
;
602 /* And patch up this file if necessary. */
603 if (subfile
->language
== language_c
604 && subfile
->next
!= NULL
605 && (subfile
->next
->language
== language_cplus
606 || subfile
->next
->language
== language_fortran
))
608 subfile
->language
= subfile
->next
->language
;
612 /* For stabs readers, the first N_SO symbol is assumed to be the
613 source file name, and the subfile struct is initialized using that
614 assumption. If another N_SO symbol is later seen, immediately
615 following the first one, then the first one is assumed to be the
616 directory name and the second one is really the source file name.
618 So we have to patch up the subfile struct by moving the old name
619 value to dirname and remembering the new name. Some sanity
620 checking is performed to ensure that the state of the subfile
621 struct is reasonable and that the old name we are assuming to be a
622 directory name actually is (by checking for a trailing '/'). */
625 patch_subfile_names (struct subfile
*subfile
, char *name
)
627 if (subfile
!= NULL
&& subfile
->dirname
== NULL
&& subfile
->name
!= NULL
628 && subfile
->name
[strlen (subfile
->name
) - 1] == '/')
630 subfile
->dirname
= subfile
->name
;
631 subfile
->name
= savestring (name
, strlen (name
));
632 last_source_file
= name
;
634 /* Default the source language to whatever can be deduced from
635 the filename. If nothing can be deduced (such as for a C/C++
636 include file with a ".h" extension), then inherit whatever
637 language the previous subfile had. This kludgery is
638 necessary because there is no standard way in some object
639 formats to record the source language. Also, when symtabs
640 are allocated we try to deduce a language then as well, but
641 it is too late for us to use that information while reading
642 symbols, since symtabs aren't allocated until after all the
643 symbols have been processed for a given source file. */
645 subfile
->language
= deduce_language_from_filename (subfile
->name
);
646 if (subfile
->language
== language_unknown
&&
647 subfile
->next
!= NULL
)
649 subfile
->language
= subfile
->next
->language
;
654 /* Handle the N_BINCL and N_EINCL symbol types that act like N_SOL for
655 switching source files (different subfiles, as we call them) within
656 one object file, but using a stack rather than in an arbitrary
662 register struct subfile_stack
*tem
663 = (struct subfile_stack
*) xmalloc (sizeof (struct subfile_stack
));
665 tem
->next
= subfile_stack
;
667 if (current_subfile
== NULL
|| current_subfile
->name
== NULL
)
671 tem
->name
= current_subfile
->name
;
678 register struct subfile_stack
*link
= subfile_stack
;
685 subfile_stack
= link
->next
;
686 free ((void *) link
);
690 /* Add a linetable entry for line number LINE and address PC to the
691 line vector for SUBFILE. */
694 record_line (register struct subfile
*subfile
, int line
, CORE_ADDR pc
)
696 struct linetable_entry
*e
;
697 /* Ignore the dummy line number in libg.o */
704 /* Make sure line vector exists and is big enough. */
705 if (!subfile
->line_vector
)
707 subfile
->line_vector_length
= INITIAL_LINE_VECTOR_LENGTH
;
708 subfile
->line_vector
= (struct linetable
*)
709 xmalloc (sizeof (struct linetable
)
710 + subfile
->line_vector_length
* sizeof (struct linetable_entry
));
711 subfile
->line_vector
->nitems
= 0;
712 have_line_numbers
= 1;
715 if (subfile
->line_vector
->nitems
+ 1 >= subfile
->line_vector_length
)
717 subfile
->line_vector_length
*= 2;
718 subfile
->line_vector
= (struct linetable
*)
719 xrealloc ((char *) subfile
->line_vector
,
720 (sizeof (struct linetable
)
721 + (subfile
->line_vector_length
722 * sizeof (struct linetable_entry
))));
725 e
= subfile
->line_vector
->item
+ subfile
->line_vector
->nitems
++;
730 /* Needed in order to sort line tables from IBM xcoff files. Sigh! */
733 compare_line_numbers (const void *ln1p
, const void *ln2p
)
735 struct linetable_entry
*ln1
= (struct linetable_entry
*) ln1p
;
736 struct linetable_entry
*ln2
= (struct linetable_entry
*) ln2p
;
738 /* Note: this code does not assume that CORE_ADDRs can fit in ints.
739 Please keep it that way. */
740 if (ln1
->pc
< ln2
->pc
)
743 if (ln1
->pc
> ln2
->pc
)
746 /* If pc equal, sort by line. I'm not sure whether this is optimum
747 behavior (see comment at struct linetable in symtab.h). */
748 return ln1
->line
- ln2
->line
;
751 /* Start a new symtab for a new source file. Called, for example,
752 when a stabs symbol of type N_SO is seen, or when a DWARF
753 TAG_compile_unit DIE is seen. It indicates the start of data for
754 one original source file. */
757 start_symtab (char *name
, char *dirname
, CORE_ADDR start_addr
)
760 last_source_file
= name
;
761 last_source_start_addr
= start_addr
;
763 global_symbols
= NULL
;
765 have_line_numbers
= 0;
767 /* Context stack is initially empty. Allocate first one with room
768 for 10 levels; reuse it forever afterward. */
769 if (context_stack
== NULL
)
771 context_stack_size
= INITIAL_CONTEXT_STACK_SIZE
;
772 context_stack
= (struct context_stack
*)
773 xmalloc (context_stack_size
* sizeof (struct context_stack
));
775 context_stack_depth
= 0;
777 /* Initialize the list of sub source files with one entry for this
778 file (the top-level source file). */
781 current_subfile
= NULL
;
782 start_subfile (name
, dirname
);
785 /* Finish the symbol definitions for one main source file, close off
786 all the lexical contexts for that file (creating struct block's for
787 them), then make the struct symtab for that file and put it in the
790 END_ADDR is the address of the end of the file's text. SECTION is
791 the section number (in objfile->section_offsets) of the blockvector
794 Note that it is possible for end_symtab() to return NULL. In
795 particular, for the DWARF case at least, it will return NULL when
796 it finds a compilation unit that has exactly one DIE, a
797 TAG_compile_unit DIE. This can happen when we link in an object
798 file that was compiled from an empty source file. Returning NULL
799 is probably not the correct thing to do, because then gdb will
800 never know about this empty file (FIXME). */
803 end_symtab (CORE_ADDR end_addr
, struct objfile
*objfile
, int section
)
805 register struct symtab
*symtab
= NULL
;
806 register struct blockvector
*blockvector
;
807 register struct subfile
*subfile
;
808 register struct context_stack
*cstk
;
809 struct subfile
*nextsub
;
811 /* Finish the lexical context of the last function in the file; pop
812 the context stack. */
814 if (context_stack_depth
> 0)
816 cstk
= pop_context ();
817 /* Make a block for the local symbols within. */
818 finish_block (cstk
->name
, &local_symbols
, cstk
->old_blocks
,
819 cstk
->start_addr
, end_addr
, objfile
);
821 if (context_stack_depth
> 0)
823 /* This is said to happen with SCO. The old coffread.c
824 code simply emptied the context stack, so we do the
825 same. FIXME: Find out why it is happening. This is not
826 believed to happen in most cases (even for coffread.c);
827 it used to be an abort(). */
828 static struct complaint msg
=
829 {"Context stack not empty in end_symtab", 0, 0};
831 context_stack_depth
= 0;
835 /* Reordered executables may have out of order pending blocks; if
836 OBJF_REORDERED is true, then sort the pending blocks. */
837 if ((objfile
->flags
& OBJF_REORDERED
) && pending_blocks
)
839 /* FIXME! Remove this horrid bubble sort and use merge sort!!! */
843 struct pending_block
*pb
, *pbnext
;
851 /* swap blocks if unordered! */
853 if (BLOCK_START (pb
->block
) < BLOCK_START (pbnext
->block
))
855 struct block
*tmp
= pb
->block
;
856 pb
->block
= pbnext
->block
;
861 pbnext
= pbnext
->next
;
867 /* Cleanup any undefined types that have been left hanging around
868 (this needs to be done before the finish_blocks so that
869 file_symbols is still good).
871 Both cleanup_undefined_types and finish_global_stabs are stabs
872 specific, but harmless for other symbol readers, since on gdb
873 startup or when finished reading stabs, the state is set so these
874 are no-ops. FIXME: Is this handled right in case of QUIT? Can
875 we make this cleaner? */
877 cleanup_undefined_types ();
878 finish_global_stabs (objfile
);
880 if (pending_blocks
== NULL
881 && file_symbols
== NULL
882 && global_symbols
== NULL
883 && have_line_numbers
== 0)
885 /* Ignore symtabs that have no functions with real debugging
891 /* Define the STATIC_BLOCK & GLOBAL_BLOCK, and build the
893 finish_block (0, &file_symbols
, 0, last_source_start_addr
, end_addr
,
895 finish_block (0, &global_symbols
, 0, last_source_start_addr
, end_addr
,
897 blockvector
= make_blockvector (objfile
);
900 #ifndef PROCESS_LINENUMBER_HOOK
901 #define PROCESS_LINENUMBER_HOOK()
903 PROCESS_LINENUMBER_HOOK (); /* Needed for xcoff. */
905 /* Now create the symtab objects proper, one for each subfile. */
906 /* (The main file is the last one on the chain.) */
908 for (subfile
= subfiles
; subfile
; subfile
= nextsub
)
910 int linetablesize
= 0;
913 /* If we have blocks of symbols, make a symtab. Otherwise, just
914 ignore this file and any line number info in it. */
917 if (subfile
->line_vector
)
919 linetablesize
= sizeof (struct linetable
) +
920 subfile
->line_vector
->nitems
* sizeof (struct linetable_entry
);
922 /* I think this is artifact from before it went on the
923 obstack. I doubt we'll need the memory between now
924 and when we free it later in this function. */
925 /* First, shrink the linetable to make more memory. */
926 subfile
->line_vector
= (struct linetable
*)
927 xrealloc ((char *) subfile
->line_vector
, linetablesize
);
930 /* Like the pending blocks, the line table may be
931 scrambled in reordered executables. Sort it if
932 OBJF_REORDERED is true. */
933 if (objfile
->flags
& OBJF_REORDERED
)
934 qsort (subfile
->line_vector
->item
,
935 subfile
->line_vector
->nitems
,
936 sizeof (struct linetable_entry
), compare_line_numbers
);
939 /* Now, allocate a symbol table. */
940 symtab
= allocate_symtab (subfile
->name
, objfile
);
942 /* Fill in its components. */
943 symtab
->blockvector
= blockvector
;
944 if (subfile
->line_vector
)
946 /* Reallocate the line table on the symbol obstack */
947 symtab
->linetable
= (struct linetable
*)
948 obstack_alloc (&objfile
->symbol_obstack
, linetablesize
);
949 memcpy (symtab
->linetable
, subfile
->line_vector
, linetablesize
);
953 symtab
->linetable
= NULL
;
955 symtab
->block_line_section
= section
;
956 if (subfile
->dirname
)
958 /* Reallocate the dirname on the symbol obstack */
959 symtab
->dirname
= (char *)
960 obstack_alloc (&objfile
->symbol_obstack
,
961 strlen (subfile
->dirname
) + 1);
962 strcpy (symtab
->dirname
, subfile
->dirname
);
966 symtab
->dirname
= NULL
;
968 symtab
->free_code
= free_linetable
;
969 symtab
->free_ptr
= NULL
;
971 /* Use whatever language we have been using for this
972 subfile, not the one that was deduced in allocate_symtab
973 from the filename. We already did our own deducing when
974 we created the subfile, and we may have altered our
975 opinion of what language it is from things we found in
977 symtab
->language
= subfile
->language
;
979 /* Save the debug format string (if any) in the symtab */
980 if (subfile
->debugformat
!= NULL
)
982 symtab
->debugformat
= obsavestring (subfile
->debugformat
,
983 strlen (subfile
->debugformat
),
984 &objfile
->symbol_obstack
);
987 /* All symtabs for the main file and the subfiles share a
988 blockvector, so we need to clear primary for everything
989 but the main file. */
993 if (subfile
->name
!= NULL
)
995 free ((void *) subfile
->name
);
997 if (subfile
->dirname
!= NULL
)
999 free ((void *) subfile
->dirname
);
1001 if (subfile
->line_vector
!= NULL
)
1003 free ((void *) subfile
->line_vector
);
1005 if (subfile
->debugformat
!= NULL
)
1007 free ((void *) subfile
->debugformat
);
1010 nextsub
= subfile
->next
;
1011 free ((void *) subfile
);
1014 /* Set this for the main source file. */
1017 symtab
->primary
= 1;
1020 last_source_file
= NULL
;
1021 current_subfile
= NULL
;
1026 /* Push a context block. Args are an identifying nesting level
1027 (checkable when you pop it), and the starting PC address of this
1030 struct context_stack
*
1031 push_context (int desc
, CORE_ADDR valu
)
1033 register struct context_stack
*new;
1035 if (context_stack_depth
== context_stack_size
)
1037 context_stack_size
*= 2;
1038 context_stack
= (struct context_stack
*)
1039 xrealloc ((char *) context_stack
,
1040 (context_stack_size
* sizeof (struct context_stack
)));
1043 new = &context_stack
[context_stack_depth
++];
1045 new->locals
= local_symbols
;
1046 new->params
= param_symbols
;
1047 new->old_blocks
= pending_blocks
;
1048 new->start_addr
= valu
;
1051 local_symbols
= NULL
;
1052 param_symbols
= NULL
;
1057 /* Compute a small integer hash code for the given name. */
1060 hashname (char *name
)
1062 register char *p
= name
;
1063 register int total
= p
[0];
1078 /* Ensure result is positive. */
1081 total
+= (1000 << 6);
1083 return (total
% HASHSIZE
);
1088 record_debugformat (char *format
)
1090 current_subfile
->debugformat
= savestring (format
, strlen (format
));
1093 /* Merge the first symbol list SRCLIST into the second symbol list
1094 TARGETLIST by repeated calls to add_symbol_to_list(). This
1095 procedure "frees" each link of SRCLIST by adding it to the
1096 free_pendings list. Caller must set SRCLIST to a null list after
1097 calling this function.
1102 merge_symbol_lists (struct pending
**srclist
, struct pending
**targetlist
)
1106 if (!srclist
|| !*srclist
)
1109 /* Merge in elements from current link. */
1110 for (i
= 0; i
< (*srclist
)->nsyms
; i
++)
1111 add_symbol_to_list ((*srclist
)->symbol
[i
], targetlist
);
1113 /* Recurse on next. */
1114 merge_symbol_lists (&(*srclist
)->next
, targetlist
);
1116 /* "Free" the current link. */
1117 (*srclist
)->next
= free_pendings
;
1118 free_pendings
= (*srclist
);
1121 /* Initialize anything that needs initializing when starting to read a
1122 fresh piece of a symbol file, e.g. reading in the stuff
1123 corresponding to a psymtab. */
1128 free_pendings
= NULL
;
1129 file_symbols
= NULL
;
1130 global_symbols
= NULL
;
1131 pending_blocks
= NULL
;
1134 /* Initialize anything that needs initializing when a completely new
1135 symbol file is specified (not just adding some symbols from another
1136 file, e.g. a shared library). */
1139 buildsym_new_init ()