1 /* Support routines for building symbol tables in GDB's internal format.
2 Copyright (C) 1986-2019 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 3 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, see <http://www.gnu.org/licenses/>. */
21 /* Standard C++ includes. */
24 /* Local non-gdb includes. */
28 #include "buildsym-legacy.h"
29 #include "complaints.h"
30 #include "cp-support.h"
32 #include "dictionary.h"
33 #include "expression.h"
34 #include "filenames.h"
35 #include "gdb_obstack.h"
42 /* For cleanup_undefined_stabs_types and finish_global_stabs (somewhat
43 questionable--see comment where we call them). */
45 #include "stabsread.h"
47 /* List of blocks already made (lexical contexts already closed).
48 This is used at the end to make the blockvector. */
52 struct pending_block
*next
;
56 static int compare_line_numbers (const void *ln1p
, const void *ln2p
);
58 /* Initial sizes of data structures. These are realloc'd larger if
59 needed, and realloc'd down to the size actually used, when
62 #define INITIAL_LINE_VECTOR_LENGTH 1000
65 buildsym_compunit::buildsym_compunit (struct objfile
*objfile_
,
67 const char *comp_dir_
,
68 enum language language_
,
70 : m_objfile (objfile_
),
71 m_last_source_file (name
== nullptr ? nullptr : xstrdup (name
)),
72 m_comp_dir (comp_dir_
== nullptr ? nullptr : xstrdup (comp_dir_
)),
73 m_language (language_
),
74 m_last_source_start_addr (last_addr
)
76 /* Allocate the compunit symtab now. The caller needs it to allocate
77 non-primary symtabs. It is also needed by get_macro_table. */
78 m_compunit_symtab
= allocate_compunit_symtab (m_objfile
, name
);
80 /* Build the subfile for NAME (the main source file) so that we can record
81 a pointer to it for later.
82 IMPORTANT: Do not allocate a struct symtab for NAME here.
83 It can happen that the debug info provides a different path to NAME than
84 DIRNAME,NAME. We cope with this in watch_main_source_file_lossage but
85 that only works if the main_subfile doesn't have a symtab yet. */
87 /* Save this so that we don't have to go looking for it at the end
88 of the subfiles list. */
89 m_main_subfile
= m_current_subfile
;
92 buildsym_compunit::~buildsym_compunit ()
94 struct subfile
*subfile
, *nextsub
;
96 if (m_pending_macros
!= nullptr)
97 free_macro_table (m_pending_macros
);
99 for (subfile
= m_subfiles
;
103 nextsub
= subfile
->next
;
104 xfree (subfile
->name
);
105 xfree (subfile
->line_vector
);
109 struct pending
*next
, *next1
;
111 for (next
= m_file_symbols
; next
!= NULL
; next
= next1
)
114 xfree ((void *) next
);
117 for (next
= m_global_symbols
; next
!= NULL
; next
= next1
)
120 xfree ((void *) next
);
125 buildsym_compunit::get_macro_table ()
127 if (m_pending_macros
== nullptr)
128 m_pending_macros
= new_macro_table (&m_objfile
->per_bfd
->storage_obstack
,
129 &m_objfile
->per_bfd
->macro_cache
,
131 return m_pending_macros
;
134 /* Maintain the lists of symbols and blocks. */
136 /* Add a symbol to one of the lists of symbols. */
139 add_symbol_to_list (struct symbol
*symbol
, struct pending
**listhead
)
141 struct pending
*link
;
143 /* If this is an alias for another symbol, don't add it. */
144 if (symbol
->ginfo
.name
&& symbol
->ginfo
.name
[0] == '#')
147 /* We keep PENDINGSIZE symbols in each link of the list. If we
148 don't have a link with room in it, add a new link. */
149 if (*listhead
== NULL
|| (*listhead
)->nsyms
== PENDINGSIZE
)
151 link
= XNEW (struct pending
);
152 link
->next
= *listhead
;
157 (*listhead
)->symbol
[(*listhead
)->nsyms
++] = symbol
;
160 /* Find a symbol named NAME on a LIST. NAME need not be
161 '\0'-terminated; LENGTH is the length of the name. */
164 find_symbol_in_list (struct pending
*list
, char *name
, int length
)
171 for (j
= list
->nsyms
; --j
>= 0;)
173 pp
= SYMBOL_LINKAGE_NAME (list
->symbol
[j
]);
174 if (*pp
== *name
&& strncmp (pp
, name
, length
) == 0
175 && pp
[length
] == '\0')
177 return (list
->symbol
[j
]);
185 /* Record BLOCK on the list of all blocks in the file. Put it after
186 OPBLOCK, or at the beginning if opblock is NULL. This puts the
187 block in the list after all its subblocks. */
190 buildsym_compunit::record_pending_block (struct block
*block
,
191 struct pending_block
*opblock
)
193 struct pending_block
*pblock
;
195 pblock
= XOBNEW (&m_pending_block_obstack
, struct pending_block
);
196 pblock
->block
= block
;
199 pblock
->next
= opblock
->next
;
200 opblock
->next
= pblock
;
204 pblock
->next
= m_pending_blocks
;
205 m_pending_blocks
= pblock
;
209 /* Take one of the lists of symbols and make a block from it. Keep
210 the order the symbols have in the list (reversed from the input
211 file). Put the block on the list of pending blocks. */
214 buildsym_compunit::finish_block_internal
215 (struct symbol
*symbol
,
216 struct pending
**listhead
,
217 struct pending_block
*old_blocks
,
218 const struct dynamic_prop
*static_link
,
219 CORE_ADDR start
, CORE_ADDR end
,
220 int is_global
, int expandable
)
222 struct gdbarch
*gdbarch
= get_objfile_arch (m_objfile
);
223 struct pending
*next
, *next1
;
225 struct pending_block
*pblock
;
226 struct pending_block
*opblock
;
229 ? allocate_global_block (&m_objfile
->objfile_obstack
)
230 : allocate_block (&m_objfile
->objfile_obstack
));
234 BLOCK_MULTIDICT (block
)
235 = mdict_create_linear (&m_objfile
->objfile_obstack
, *listhead
);
241 BLOCK_MULTIDICT (block
) = mdict_create_hashed_expandable (m_language
);
242 mdict_add_pending (BLOCK_MULTIDICT (block
), *listhead
);
246 BLOCK_MULTIDICT (block
) =
247 mdict_create_hashed (&m_objfile
->objfile_obstack
, *listhead
);
251 BLOCK_START (block
) = start
;
252 BLOCK_END (block
) = end
;
254 /* Put the block in as the value of the symbol that names it. */
258 struct type
*ftype
= SYMBOL_TYPE (symbol
);
259 struct mdict_iterator miter
;
260 SYMBOL_BLOCK_VALUE (symbol
) = block
;
261 BLOCK_FUNCTION (block
) = symbol
;
263 if (TYPE_NFIELDS (ftype
) <= 0)
265 /* No parameter type information is recorded with the
266 function's type. Set that from the type of the
267 parameter symbols. */
268 int nparams
= 0, iparams
;
271 /* Here we want to directly access the dictionary, because
272 we haven't fully initialized the block yet. */
273 ALL_DICT_SYMBOLS (BLOCK_MULTIDICT (block
), miter
, sym
)
275 if (SYMBOL_IS_ARGUMENT (sym
))
280 TYPE_NFIELDS (ftype
) = nparams
;
281 TYPE_FIELDS (ftype
) = (struct field
*)
282 TYPE_ALLOC (ftype
, nparams
* sizeof (struct field
));
285 /* Here we want to directly access the dictionary, because
286 we haven't fully initialized the block yet. */
287 ALL_DICT_SYMBOLS (BLOCK_MULTIDICT (block
), miter
, sym
)
289 if (iparams
== nparams
)
292 if (SYMBOL_IS_ARGUMENT (sym
))
294 TYPE_FIELD_TYPE (ftype
, iparams
) = SYMBOL_TYPE (sym
);
295 TYPE_FIELD_ARTIFICIAL (ftype
, iparams
) = 0;
304 BLOCK_FUNCTION (block
) = NULL
;
307 if (static_link
!= NULL
)
308 objfile_register_static_link (m_objfile
, block
, static_link
);
310 /* Now free the links of the list, and empty the list. */
312 for (next
= *listhead
; next
; next
= next1
)
319 /* Check to be sure that the blocks have an end address that is
320 greater than starting address. */
322 if (BLOCK_END (block
) < BLOCK_START (block
))
326 complaint (_("block end address less than block "
327 "start address in %s (patched it)"),
328 SYMBOL_PRINT_NAME (symbol
));
332 complaint (_("block end address %s less than block "
333 "start address %s (patched it)"),
334 paddress (gdbarch
, BLOCK_END (block
)),
335 paddress (gdbarch
, BLOCK_START (block
)));
337 /* Better than nothing. */
338 BLOCK_END (block
) = BLOCK_START (block
);
341 /* Install this block as the superblock of all blocks made since the
342 start of this scope that don't have superblocks yet. */
345 for (pblock
= m_pending_blocks
;
346 pblock
&& pblock
!= old_blocks
;
347 pblock
= pblock
->next
)
349 if (BLOCK_SUPERBLOCK (pblock
->block
) == NULL
)
351 /* Check to be sure the blocks are nested as we receive
352 them. If the compiler/assembler/linker work, this just
353 burns a small amount of time.
355 Skip blocks which correspond to a function; they're not
356 physically nested inside this other blocks, only
358 if (BLOCK_FUNCTION (pblock
->block
) == NULL
359 && (BLOCK_START (pblock
->block
) < BLOCK_START (block
)
360 || BLOCK_END (pblock
->block
) > BLOCK_END (block
)))
364 complaint (_("inner block not inside outer block in %s"),
365 SYMBOL_PRINT_NAME (symbol
));
369 complaint (_("inner block (%s-%s) not "
370 "inside outer block (%s-%s)"),
371 paddress (gdbarch
, BLOCK_START (pblock
->block
)),
372 paddress (gdbarch
, BLOCK_END (pblock
->block
)),
373 paddress (gdbarch
, BLOCK_START (block
)),
374 paddress (gdbarch
, BLOCK_END (block
)));
376 if (BLOCK_START (pblock
->block
) < BLOCK_START (block
))
377 BLOCK_START (pblock
->block
) = BLOCK_START (block
);
378 if (BLOCK_END (pblock
->block
) > BLOCK_END (block
))
379 BLOCK_END (pblock
->block
) = BLOCK_END (block
);
381 BLOCK_SUPERBLOCK (pblock
->block
) = block
;
386 block_set_using (block
,
388 ? m_global_using_directives
389 : m_local_using_directives
),
390 &m_objfile
->objfile_obstack
);
392 m_global_using_directives
= NULL
;
394 m_local_using_directives
= NULL
;
396 record_pending_block (block
, opblock
);
402 buildsym_compunit::finish_block (struct symbol
*symbol
,
403 struct pending_block
*old_blocks
,
404 const struct dynamic_prop
*static_link
,
405 CORE_ADDR start
, CORE_ADDR end
)
407 return finish_block_internal (symbol
, &m_local_symbols
,
408 old_blocks
, static_link
, start
, end
, 0, 0);
411 /* Record that the range of addresses from START to END_INCLUSIVE
412 (inclusive, like it says) belongs to BLOCK. BLOCK's start and end
413 addresses must be set already. You must apply this function to all
414 BLOCK's children before applying it to BLOCK.
416 If a call to this function complicates the picture beyond that
417 already provided by BLOCK_START and BLOCK_END, then we create an
418 address map for the block. */
420 buildsym_compunit::record_block_range (struct block
*block
,
422 CORE_ADDR end_inclusive
)
424 /* If this is any different from the range recorded in the block's
425 own BLOCK_START and BLOCK_END, then note that the address map has
426 become interesting. Note that even if this block doesn't have
427 any "interesting" ranges, some later block might, so we still
428 need to record this block in the addrmap. */
429 if (start
!= BLOCK_START (block
)
430 || end_inclusive
+ 1 != BLOCK_END (block
))
431 m_pending_addrmap_interesting
= true;
433 if (m_pending_addrmap
== nullptr)
434 m_pending_addrmap
= addrmap_create_mutable (&m_pending_addrmap_obstack
);
436 addrmap_set_empty (m_pending_addrmap
, start
, end_inclusive
, block
);
440 buildsym_compunit::make_blockvector ()
442 struct pending_block
*next
;
443 struct blockvector
*blockvector
;
446 /* Count the length of the list of blocks. */
448 for (next
= m_pending_blocks
, i
= 0; next
; next
= next
->next
, i
++)
452 blockvector
= (struct blockvector
*)
453 obstack_alloc (&m_objfile
->objfile_obstack
,
454 (sizeof (struct blockvector
)
455 + (i
- 1) * sizeof (struct block
*)));
457 /* Copy the blocks into the blockvector. This is done in reverse
458 order, which happens to put the blocks into the proper order
459 (ascending starting address). finish_block has hair to insert
460 each block into the list after its subblocks in order to make
461 sure this is true. */
463 BLOCKVECTOR_NBLOCKS (blockvector
) = i
;
464 for (next
= m_pending_blocks
; next
; next
= next
->next
)
466 BLOCKVECTOR_BLOCK (blockvector
, --i
) = next
->block
;
469 free_pending_blocks ();
471 /* If we needed an address map for this symtab, record it in the
473 if (m_pending_addrmap
!= nullptr && m_pending_addrmap_interesting
)
474 BLOCKVECTOR_MAP (blockvector
)
475 = addrmap_create_fixed (m_pending_addrmap
, &m_objfile
->objfile_obstack
);
477 BLOCKVECTOR_MAP (blockvector
) = 0;
479 /* Some compilers output blocks in the wrong order, but we depend on
480 their being in the right order so we can binary search. Check the
481 order and moan about it.
482 Note: Remember that the first two blocks are the global and static
483 blocks. We could special case that fact and begin checking at block 2.
484 To avoid making that assumption we do not. */
485 if (BLOCKVECTOR_NBLOCKS (blockvector
) > 1)
487 for (i
= 1; i
< BLOCKVECTOR_NBLOCKS (blockvector
); i
++)
489 if (BLOCK_START (BLOCKVECTOR_BLOCK (blockvector
, i
- 1))
490 > BLOCK_START (BLOCKVECTOR_BLOCK (blockvector
, i
)))
493 = BLOCK_START (BLOCKVECTOR_BLOCK (blockvector
, i
));
495 complaint (_("block at %s out of order"),
496 hex_string ((LONGEST
) start
));
501 return (blockvector
);
504 /* Start recording information about source code that came from an
505 included (or otherwise merged-in) source file with a different
506 name. NAME is the name of the file (cannot be NULL). */
509 buildsym_compunit::start_subfile (const char *name
)
511 const char *subfile_dirname
;
512 struct subfile
*subfile
;
514 subfile_dirname
= m_comp_dir
.get ();
516 /* See if this subfile is already registered. */
518 for (subfile
= m_subfiles
; subfile
; subfile
= subfile
->next
)
522 /* If NAME is an absolute path, and this subfile is not, then
523 attempt to create an absolute path to compare. */
524 if (IS_ABSOLUTE_PATH (name
)
525 && !IS_ABSOLUTE_PATH (subfile
->name
)
526 && subfile_dirname
!= NULL
)
527 subfile_name
= concat (subfile_dirname
, SLASH_STRING
,
528 subfile
->name
, (char *) NULL
);
530 subfile_name
= subfile
->name
;
532 if (FILENAME_CMP (subfile_name
, name
) == 0)
534 m_current_subfile
= subfile
;
535 if (subfile_name
!= subfile
->name
)
536 xfree (subfile_name
);
539 if (subfile_name
!= subfile
->name
)
540 xfree (subfile_name
);
543 /* This subfile is not known. Add an entry for it. */
545 subfile
= XNEW (struct subfile
);
546 memset (subfile
, 0, sizeof (struct subfile
));
547 subfile
->buildsym_compunit
= this;
549 subfile
->next
= m_subfiles
;
550 m_subfiles
= subfile
;
552 m_current_subfile
= subfile
;
554 subfile
->name
= xstrdup (name
);
556 /* Initialize line-number recording for this subfile. */
557 subfile
->line_vector
= NULL
;
559 /* Default the source language to whatever can be deduced from the
560 filename. If nothing can be deduced (such as for a C/C++ include
561 file with a ".h" extension), then inherit whatever language the
562 previous subfile had. This kludgery is necessary because there
563 is no standard way in some object formats to record the source
564 language. Also, when symtabs are allocated we try to deduce a
565 language then as well, but it is too late for us to use that
566 information while reading symbols, since symtabs aren't allocated
567 until after all the symbols have been processed for a given
570 subfile
->language
= deduce_language_from_filename (subfile
->name
);
571 if (subfile
->language
== language_unknown
572 && subfile
->next
!= NULL
)
574 subfile
->language
= subfile
->next
->language
;
577 /* If the filename of this subfile ends in .C, then change the
578 language of any pending subfiles from C to C++. We also accept
579 any other C++ suffixes accepted by deduce_language_from_filename. */
580 /* Likewise for f2c. */
585 enum language sublang
= deduce_language_from_filename (subfile
->name
);
587 if (sublang
== language_cplus
|| sublang
== language_fortran
)
588 for (s
= m_subfiles
; s
!= NULL
; s
= s
->next
)
589 if (s
->language
== language_c
)
590 s
->language
= sublang
;
593 /* And patch up this file if necessary. */
594 if (subfile
->language
== language_c
595 && subfile
->next
!= NULL
596 && (subfile
->next
->language
== language_cplus
597 || subfile
->next
->language
== language_fortran
))
599 subfile
->language
= subfile
->next
->language
;
603 /* For stabs readers, the first N_SO symbol is assumed to be the
604 source file name, and the subfile struct is initialized using that
605 assumption. If another N_SO symbol is later seen, immediately
606 following the first one, then the first one is assumed to be the
607 directory name and the second one is really the source file name.
609 So we have to patch up the subfile struct by moving the old name
610 value to dirname and remembering the new name. Some sanity
611 checking is performed to ensure that the state of the subfile
612 struct is reasonable and that the old name we are assuming to be a
613 directory name actually is (by checking for a trailing '/'). */
616 buildsym_compunit::patch_subfile_names (struct subfile
*subfile
,
620 && m_comp_dir
== NULL
621 && subfile
->name
!= NULL
622 && IS_DIR_SEPARATOR (subfile
->name
[strlen (subfile
->name
) - 1]))
624 m_comp_dir
.reset (subfile
->name
);
625 subfile
->name
= xstrdup (name
);
626 set_last_source_file (name
);
628 /* Default the source language to whatever can be deduced from
629 the filename. If nothing can be deduced (such as for a C/C++
630 include file with a ".h" extension), then inherit whatever
631 language the previous subfile had. This kludgery is
632 necessary because there is no standard way in some object
633 formats to record the source language. Also, when symtabs
634 are allocated we try to deduce a language then as well, but
635 it is too late for us to use that information while reading
636 symbols, since symtabs aren't allocated until after all the
637 symbols have been processed for a given source file. */
639 subfile
->language
= deduce_language_from_filename (subfile
->name
);
640 if (subfile
->language
== language_unknown
641 && subfile
->next
!= NULL
)
643 subfile
->language
= subfile
->next
->language
;
648 /* Handle the N_BINCL and N_EINCL symbol types that act like N_SOL for
649 switching source files (different subfiles, as we call them) within
650 one object file, but using a stack rather than in an arbitrary
654 buildsym_compunit::push_subfile ()
656 gdb_assert (m_current_subfile
!= NULL
);
657 gdb_assert (m_current_subfile
->name
!= NULL
);
658 m_subfile_stack
.push_back (m_current_subfile
->name
);
662 buildsym_compunit::pop_subfile ()
664 gdb_assert (!m_subfile_stack
.empty ());
665 const char *name
= m_subfile_stack
.back ();
666 m_subfile_stack
.pop_back ();
670 /* Add a linetable entry for line number LINE and address PC to the
671 line vector for SUBFILE. */
674 buildsym_compunit::record_line (struct subfile
*subfile
, int line
,
677 struct linetable_entry
*e
;
679 /* Ignore the dummy line number in libg.o */
685 /* Make sure line vector exists and is big enough. */
686 if (!subfile
->line_vector
)
688 subfile
->line_vector_length
= INITIAL_LINE_VECTOR_LENGTH
;
689 subfile
->line_vector
= (struct linetable
*)
690 xmalloc (sizeof (struct linetable
)
691 + subfile
->line_vector_length
* sizeof (struct linetable_entry
));
692 subfile
->line_vector
->nitems
= 0;
693 m_have_line_numbers
= true;
696 if (subfile
->line_vector
->nitems
+ 1 >= subfile
->line_vector_length
)
698 subfile
->line_vector_length
*= 2;
699 subfile
->line_vector
= (struct linetable
*)
700 xrealloc ((char *) subfile
->line_vector
,
701 (sizeof (struct linetable
)
702 + (subfile
->line_vector_length
703 * sizeof (struct linetable_entry
))));
706 /* Normally, we treat lines as unsorted. But the end of sequence
707 marker is special. We sort line markers at the same PC by line
708 number, so end of sequence markers (which have line == 0) appear
709 first. This is right if the marker ends the previous function,
710 and there is no padding before the next function. But it is
711 wrong if the previous line was empty and we are now marking a
712 switch to a different subfile. We must leave the end of sequence
713 marker at the end of this group of lines, not sort the empty line
714 to after the marker. The easiest way to accomplish this is to
715 delete any empty lines from our table, if they are followed by
716 end of sequence markers. All we lose is the ability to set
717 breakpoints at some lines which contain no instructions
719 if (line
== 0 && subfile
->line_vector
->nitems
> 0)
721 e
= subfile
->line_vector
->item
+ subfile
->line_vector
->nitems
- 1;
722 while (subfile
->line_vector
->nitems
> 0 && e
->pc
== pc
)
725 subfile
->line_vector
->nitems
--;
729 e
= subfile
->line_vector
->item
+ subfile
->line_vector
->nitems
++;
734 /* Needed in order to sort line tables from IBM xcoff files. Sigh! */
737 compare_line_numbers (const void *ln1p
, const void *ln2p
)
739 struct linetable_entry
*ln1
= (struct linetable_entry
*) ln1p
;
740 struct linetable_entry
*ln2
= (struct linetable_entry
*) ln2p
;
742 /* Note: this code does not assume that CORE_ADDRs can fit in ints.
743 Please keep it that way. */
744 if (ln1
->pc
< ln2
->pc
)
747 if (ln1
->pc
> ln2
->pc
)
750 /* If pc equal, sort by line. I'm not sure whether this is optimum
751 behavior (see comment at struct linetable in symtab.h). */
752 return ln1
->line
- ln2
->line
;
755 /* Subroutine of end_symtab to simplify it. Look for a subfile that
756 matches the main source file's basename. If there is only one, and
757 if the main source file doesn't have any symbol or line number
758 information, then copy this file's symtab and line_vector to the
759 main source file's subfile and discard the other subfile. This can
760 happen because of a compiler bug or from the user playing games
761 with #line or from things like a distributed build system that
762 manipulates the debug info. This can also happen from an innocent
763 symlink in the paths, we don't canonicalize paths here. */
766 buildsym_compunit::watch_main_source_file_lossage ()
768 struct subfile
*mainsub
, *subfile
;
770 /* Get the main source file. */
771 mainsub
= m_main_subfile
;
773 /* If the main source file doesn't have any line number or symbol
774 info, look for an alias in another subfile. */
776 if (mainsub
->line_vector
== NULL
777 && mainsub
->symtab
== NULL
)
779 const char *mainbase
= lbasename (mainsub
->name
);
781 struct subfile
*prevsub
;
782 struct subfile
*mainsub_alias
= NULL
;
783 struct subfile
*prev_mainsub_alias
= NULL
;
786 for (subfile
= m_subfiles
;
788 subfile
= subfile
->next
)
790 if (subfile
== mainsub
)
792 if (filename_cmp (lbasename (subfile
->name
), mainbase
) == 0)
795 mainsub_alias
= subfile
;
796 prev_mainsub_alias
= prevsub
;
803 gdb_assert (mainsub_alias
!= NULL
&& mainsub_alias
!= mainsub
);
805 /* Found a match for the main source file.
806 Copy its line_vector and symtab to the main subfile
807 and then discard it. */
809 mainsub
->line_vector
= mainsub_alias
->line_vector
;
810 mainsub
->line_vector_length
= mainsub_alias
->line_vector_length
;
811 mainsub
->symtab
= mainsub_alias
->symtab
;
813 if (prev_mainsub_alias
== NULL
)
814 m_subfiles
= mainsub_alias
->next
;
816 prev_mainsub_alias
->next
= mainsub_alias
->next
;
817 xfree (mainsub_alias
->name
);
818 xfree (mainsub_alias
);
823 /* Implementation of the first part of end_symtab. It allows modifying
824 STATIC_BLOCK before it gets finalized by end_symtab_from_static_block.
825 If the returned value is NULL there is no blockvector created for
826 this symtab (you still must call end_symtab_from_static_block).
828 END_ADDR is the same as for end_symtab: the address of the end of the
831 If EXPANDABLE is non-zero the STATIC_BLOCK dictionary is made
834 If REQUIRED is non-zero, then a symtab is created even if it does
835 not contain any symbols. */
838 buildsym_compunit::end_symtab_get_static_block (CORE_ADDR end_addr
,
839 int expandable
, int required
)
841 /* Finish the lexical context of the last function in the file; pop
842 the context stack. */
844 if (!m_context_stack
.empty ())
846 struct context_stack cstk
= pop_context ();
848 /* Make a block for the local symbols within. */
849 finish_block (cstk
.name
, cstk
.old_blocks
, NULL
,
850 cstk
.start_addr
, end_addr
);
852 if (!m_context_stack
.empty ())
854 /* This is said to happen with SCO. The old coffread.c
855 code simply emptied the context stack, so we do the
856 same. FIXME: Find out why it is happening. This is not
857 believed to happen in most cases (even for coffread.c);
858 it used to be an abort(). */
859 complaint (_("Context stack not empty in end_symtab"));
860 m_context_stack
.clear ();
864 /* Reordered executables may have out of order pending blocks; if
865 OBJF_REORDERED is true, then sort the pending blocks. */
867 if ((m_objfile
->flags
& OBJF_REORDERED
) && m_pending_blocks
)
869 struct pending_block
*pb
;
871 std::vector
<block
*> barray
;
873 for (pb
= m_pending_blocks
; pb
!= NULL
; pb
= pb
->next
)
874 barray
.push_back (pb
->block
);
876 /* Sort blocks by start address in descending order. Blocks with the
877 same start address must remain in the original order to preserve
878 inline function caller/callee relationships. */
879 std::stable_sort (barray
.begin (), barray
.end (),
880 [] (const block
*a
, const block
*b
)
882 return BLOCK_START (a
) > BLOCK_START (b
);
886 for (pb
= m_pending_blocks
; pb
!= NULL
; pb
= pb
->next
)
887 pb
->block
= barray
[i
++];
890 /* Cleanup any undefined types that have been left hanging around
891 (this needs to be done before the finish_blocks so that
892 file_symbols is still good).
894 Both cleanup_undefined_stabs_types and finish_global_stabs are stabs
895 specific, but harmless for other symbol readers, since on gdb
896 startup or when finished reading stabs, the state is set so these
897 are no-ops. FIXME: Is this handled right in case of QUIT? Can
898 we make this cleaner? */
900 cleanup_undefined_stabs_types (m_objfile
);
901 finish_global_stabs (m_objfile
);
904 && m_pending_blocks
== NULL
905 && m_file_symbols
== NULL
906 && m_global_symbols
== NULL
907 && !m_have_line_numbers
908 && m_pending_macros
== NULL
909 && m_global_using_directives
== NULL
)
911 /* Ignore symtabs that have no functions with real debugging info. */
916 /* Define the STATIC_BLOCK. */
917 return finish_block_internal (NULL
, get_file_symbols (), NULL
, NULL
,
918 m_last_source_start_addr
,
919 end_addr
, 0, expandable
);
923 /* Subroutine of end_symtab_from_static_block to simplify it.
924 Handle the "have blockvector" case.
925 See end_symtab_from_static_block for a description of the arguments. */
927 struct compunit_symtab
*
928 buildsym_compunit::end_symtab_with_blockvector (struct block
*static_block
,
929 int section
, int expandable
)
931 struct compunit_symtab
*cu
= m_compunit_symtab
;
932 struct blockvector
*blockvector
;
933 struct subfile
*subfile
;
936 gdb_assert (static_block
!= NULL
);
937 gdb_assert (m_subfiles
!= NULL
);
939 end_addr
= BLOCK_END (static_block
);
941 /* Create the GLOBAL_BLOCK and build the blockvector. */
942 finish_block_internal (NULL
, get_global_symbols (), NULL
, NULL
,
943 m_last_source_start_addr
, end_addr
,
945 blockvector
= make_blockvector ();
947 /* Read the line table if it has to be read separately.
948 This is only used by xcoffread.c. */
949 if (m_objfile
->sf
->sym_read_linetable
!= NULL
)
950 m_objfile
->sf
->sym_read_linetable (m_objfile
);
952 /* Handle the case where the debug info specifies a different path
953 for the main source file. It can cause us to lose track of its
954 line number information. */
955 watch_main_source_file_lossage ();
957 /* Now create the symtab objects proper, if not already done,
958 one for each subfile. */
960 for (subfile
= m_subfiles
;
962 subfile
= subfile
->next
)
964 int linetablesize
= 0;
966 if (subfile
->line_vector
)
968 linetablesize
= sizeof (struct linetable
) +
969 subfile
->line_vector
->nitems
* sizeof (struct linetable_entry
);
971 /* Like the pending blocks, the line table may be
972 scrambled in reordered executables. Sort it if
973 OBJF_REORDERED is true. */
974 if (m_objfile
->flags
& OBJF_REORDERED
)
975 qsort (subfile
->line_vector
->item
,
976 subfile
->line_vector
->nitems
,
977 sizeof (struct linetable_entry
), compare_line_numbers
);
980 /* Allocate a symbol table if necessary. */
981 if (subfile
->symtab
== NULL
)
982 subfile
->symtab
= allocate_symtab (cu
, subfile
->name
);
983 struct symtab
*symtab
= subfile
->symtab
;
985 /* Fill in its components. */
987 if (subfile
->line_vector
)
989 /* Reallocate the line table on the symbol obstack. */
990 SYMTAB_LINETABLE (symtab
) = (struct linetable
*)
991 obstack_alloc (&m_objfile
->objfile_obstack
, linetablesize
);
992 memcpy (SYMTAB_LINETABLE (symtab
), subfile
->line_vector
,
997 SYMTAB_LINETABLE (symtab
) = NULL
;
1000 /* Use whatever language we have been using for this
1001 subfile, not the one that was deduced in allocate_symtab
1002 from the filename. We already did our own deducing when
1003 we created the subfile, and we may have altered our
1004 opinion of what language it is from things we found in
1006 symtab
->language
= subfile
->language
;
1009 /* Make sure the symtab of main_subfile is the first in its list. */
1011 struct symtab
*main_symtab
, *prev_symtab
;
1013 main_symtab
= m_main_subfile
->symtab
;
1015 for (symtab
*symtab
: compunit_filetabs (cu
))
1017 if (symtab
== main_symtab
)
1019 if (prev_symtab
!= NULL
)
1021 prev_symtab
->next
= main_symtab
->next
;
1022 main_symtab
->next
= COMPUNIT_FILETABS (cu
);
1023 COMPUNIT_FILETABS (cu
) = main_symtab
;
1027 prev_symtab
= symtab
;
1029 gdb_assert (main_symtab
== COMPUNIT_FILETABS (cu
));
1032 /* Fill out the compunit symtab. */
1034 if (m_comp_dir
!= NULL
)
1036 /* Reallocate the dirname on the symbol obstack. */
1037 const char *comp_dir
= m_comp_dir
.get ();
1038 COMPUNIT_DIRNAME (cu
)
1039 = (const char *) obstack_copy0 (&m_objfile
->objfile_obstack
,
1040 comp_dir
, strlen (comp_dir
));
1043 /* Save the debug format string (if any) in the symtab. */
1044 COMPUNIT_DEBUGFORMAT (cu
) = m_debugformat
;
1046 /* Similarly for the producer. */
1047 COMPUNIT_PRODUCER (cu
) = m_producer
;
1049 COMPUNIT_BLOCKVECTOR (cu
) = blockvector
;
1051 struct block
*b
= BLOCKVECTOR_BLOCK (blockvector
, GLOBAL_BLOCK
);
1053 set_block_compunit_symtab (b
, cu
);
1056 COMPUNIT_BLOCK_LINE_SECTION (cu
) = section
;
1058 COMPUNIT_MACRO_TABLE (cu
) = release_macros ();
1060 /* Default any symbols without a specified symtab to the primary symtab. */
1064 /* The main source file's symtab. */
1065 struct symtab
*symtab
= COMPUNIT_FILETABS (cu
);
1067 for (block_i
= 0; block_i
< BLOCKVECTOR_NBLOCKS (blockvector
); block_i
++)
1069 struct block
*block
= BLOCKVECTOR_BLOCK (blockvector
, block_i
);
1071 struct mdict_iterator miter
;
1073 /* Inlined functions may have symbols not in the global or
1074 static symbol lists. */
1075 if (BLOCK_FUNCTION (block
) != NULL
)
1076 if (symbol_symtab (BLOCK_FUNCTION (block
)) == NULL
)
1077 symbol_set_symtab (BLOCK_FUNCTION (block
), symtab
);
1079 /* Note that we only want to fix up symbols from the local
1080 blocks, not blocks coming from included symtabs. That is why
1081 we use ALL_DICT_SYMBOLS here and not ALL_BLOCK_SYMBOLS. */
1082 ALL_DICT_SYMBOLS (BLOCK_MULTIDICT (block
), miter
, sym
)
1083 if (symbol_symtab (sym
) == NULL
)
1084 symbol_set_symtab (sym
, symtab
);
1088 add_compunit_symtab_to_objfile (cu
);
1093 /* Implementation of the second part of end_symtab. Pass STATIC_BLOCK
1094 as value returned by end_symtab_get_static_block.
1096 SECTION is the same as for end_symtab: the section number
1097 (in objfile->section_offsets) of the blockvector and linetable.
1099 If EXPANDABLE is non-zero the GLOBAL_BLOCK dictionary is made
1102 struct compunit_symtab
*
1103 buildsym_compunit::end_symtab_from_static_block (struct block
*static_block
,
1104 int section
, int expandable
)
1106 struct compunit_symtab
*cu
;
1108 if (static_block
== NULL
)
1110 /* Handle the "no blockvector" case.
1111 When this happens there is nothing to record, so there's nothing
1112 to do: memory will be freed up later.
1114 Note: We won't be adding a compunit to the objfile's list of
1115 compunits, so there's nothing to unchain. However, since each symtab
1116 is added to the objfile's obstack we can't free that space.
1117 We could do better, but this is believed to be a sufficiently rare
1122 cu
= end_symtab_with_blockvector (static_block
, section
, expandable
);
1127 /* Finish the symbol definitions for one main source file, close off
1128 all the lexical contexts for that file (creating struct block's for
1129 them), then make the struct symtab for that file and put it in the
1132 END_ADDR is the address of the end of the file's text. SECTION is
1133 the section number (in objfile->section_offsets) of the blockvector
1136 Note that it is possible for end_symtab() to return NULL. In
1137 particular, for the DWARF case at least, it will return NULL when
1138 it finds a compilation unit that has exactly one DIE, a
1139 TAG_compile_unit DIE. This can happen when we link in an object
1140 file that was compiled from an empty source file. Returning NULL
1141 is probably not the correct thing to do, because then gdb will
1142 never know about this empty file (FIXME).
1144 If you need to modify STATIC_BLOCK before it is finalized you should
1145 call end_symtab_get_static_block and end_symtab_from_static_block
1148 struct compunit_symtab
*
1149 buildsym_compunit::end_symtab (CORE_ADDR end_addr
, int section
)
1151 struct block
*static_block
;
1153 static_block
= end_symtab_get_static_block (end_addr
, 0, 0);
1154 return end_symtab_from_static_block (static_block
, section
, 0);
1157 /* Same as end_symtab except create a symtab that can be later added to. */
1159 struct compunit_symtab
*
1160 buildsym_compunit::end_expandable_symtab (CORE_ADDR end_addr
, int section
)
1162 struct block
*static_block
;
1164 static_block
= end_symtab_get_static_block (end_addr
, 1, 0);
1165 return end_symtab_from_static_block (static_block
, section
, 1);
1168 /* Subroutine of augment_type_symtab to simplify it.
1169 Attach the main source file's symtab to all symbols in PENDING_LIST that
1173 set_missing_symtab (struct pending
*pending_list
,
1174 struct compunit_symtab
*cu
)
1176 struct pending
*pending
;
1179 for (pending
= pending_list
; pending
!= NULL
; pending
= pending
->next
)
1181 for (i
= 0; i
< pending
->nsyms
; ++i
)
1183 if (symbol_symtab (pending
->symbol
[i
]) == NULL
)
1184 symbol_set_symtab (pending
->symbol
[i
], COMPUNIT_FILETABS (cu
));
1189 /* Same as end_symtab, but for the case where we're adding more symbols
1190 to an existing symtab that is known to contain only type information.
1191 This is the case for DWARF4 Type Units. */
1194 buildsym_compunit::augment_type_symtab ()
1196 struct compunit_symtab
*cust
= m_compunit_symtab
;
1197 const struct blockvector
*blockvector
= COMPUNIT_BLOCKVECTOR (cust
);
1199 if (!m_context_stack
.empty ())
1200 complaint (_("Context stack not empty in augment_type_symtab"));
1201 if (m_pending_blocks
!= NULL
)
1202 complaint (_("Blocks in a type symtab"));
1203 if (m_pending_macros
!= NULL
)
1204 complaint (_("Macro in a type symtab"));
1205 if (m_have_line_numbers
)
1206 complaint (_("Line numbers recorded in a type symtab"));
1208 if (m_file_symbols
!= NULL
)
1210 struct block
*block
= BLOCKVECTOR_BLOCK (blockvector
, STATIC_BLOCK
);
1212 /* First mark any symbols without a specified symtab as belonging
1213 to the primary symtab. */
1214 set_missing_symtab (m_file_symbols
, cust
);
1216 mdict_add_pending (BLOCK_MULTIDICT (block
), m_file_symbols
);
1219 if (m_global_symbols
!= NULL
)
1221 struct block
*block
= BLOCKVECTOR_BLOCK (blockvector
, GLOBAL_BLOCK
);
1223 /* First mark any symbols without a specified symtab as belonging
1224 to the primary symtab. */
1225 set_missing_symtab (m_global_symbols
, cust
);
1227 mdict_add_pending (BLOCK_MULTIDICT (block
),
1232 /* Push a context block. Args are an identifying nesting level
1233 (checkable when you pop it), and the starting PC address of this
1236 struct context_stack
*
1237 buildsym_compunit::push_context (int desc
, CORE_ADDR valu
)
1239 m_context_stack
.emplace_back ();
1240 struct context_stack
*newobj
= &m_context_stack
.back ();
1242 newobj
->depth
= desc
;
1243 newobj
->locals
= m_local_symbols
;
1244 newobj
->old_blocks
= m_pending_blocks
;
1245 newobj
->start_addr
= valu
;
1246 newobj
->local_using_directives
= m_local_using_directives
;
1247 newobj
->name
= NULL
;
1249 m_local_symbols
= NULL
;
1250 m_local_using_directives
= NULL
;
1255 /* Pop a context block. Returns the address of the context block just
1258 struct context_stack
1259 buildsym_compunit::pop_context ()
1261 gdb_assert (!m_context_stack
.empty ());
1262 struct context_stack result
= m_context_stack
.back ();
1263 m_context_stack
.pop_back ();