1 /* Support routines for building symbol tables in GDB's internal format.
2 Copyright (C) 1986-2020 Free Software Foundation, Inc.
3 Copyright (C) 2019-2020 Advanced Micro Devices, Inc. All rights reserved.
5 This file is part of GDB.
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.
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.
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/>. */
21 #include "buildsym-legacy.h"
23 #include "gdb_obstack.h"
28 #include "complaints.h"
29 #include "expression.h" /* For "enum exp_opcode" used by... */
30 #include "filenames.h" /* For DOSish file names. */
32 #include "demangle.h" /* Needed by SYMBOL_INIT_DEMANGLED_NAME. */
34 #include "cp-support.h"
35 #include "dictionary.h"
39 /* For cleanup_undefined_stabs_types and finish_global_stabs (somewhat
40 questionable--see comment where we call them). */
42 #include "stabsread.h"
44 /* List of blocks already made (lexical contexts already closed).
45 This is used at the end to make the blockvector. */
49 struct pending_block
*next
;
53 /* Initial sizes of data structures. These are realloc'd larger if
54 needed, and realloc'd down to the size actually used, when
57 #define INITIAL_LINE_VECTOR_LENGTH 1000
60 buildsym_compunit::buildsym_compunit (struct objfile
*objfile_
,
62 const char *comp_dir_
,
63 enum language language_
,
65 : m_objfile (objfile_
),
66 m_last_source_file (name
== nullptr ? nullptr : xstrdup (name
)),
67 m_comp_dir (comp_dir_
== nullptr ? nullptr : xstrdup (comp_dir_
)),
68 m_language (language_
),
69 m_last_source_start_addr (last_addr
)
71 /* Allocate the compunit symtab now. The caller needs it to allocate
72 non-primary symtabs. It is also needed by get_macro_table. */
73 m_compunit_symtab
= allocate_compunit_symtab (m_objfile
, name
);
75 /* Build the subfile for NAME (the main source file) so that we can record
76 a pointer to it for later.
77 IMPORTANT: Do not allocate a struct symtab for NAME here.
78 It can happen that the debug info provides a different path to NAME than
79 DIRNAME,NAME. We cope with this in watch_main_source_file_lossage but
80 that only works if the main_subfile doesn't have a symtab yet. */
82 /* Save this so that we don't have to go looking for it at the end
83 of the subfiles list. */
84 m_main_subfile
= m_current_subfile
;
87 buildsym_compunit::~buildsym_compunit ()
89 struct subfile
*subfile
, *nextsub
;
91 if (m_pending_macros
!= nullptr)
92 free_macro_table (m_pending_macros
);
94 for (subfile
= m_subfiles
;
98 nextsub
= subfile
->next
;
99 xfree (subfile
->name
);
100 xfree (subfile
->line_vector
);
104 struct pending
*next
, *next1
;
106 for (next
= m_file_symbols
; next
!= NULL
; next
= next1
)
109 xfree ((void *) next
);
112 for (next
= m_global_symbols
; next
!= NULL
; next
= next1
)
115 xfree ((void *) next
);
120 buildsym_compunit::get_macro_table ()
122 if (m_pending_macros
== nullptr)
123 m_pending_macros
= new_macro_table (&m_objfile
->per_bfd
->storage_obstack
,
124 &m_objfile
->per_bfd
->macro_cache
,
126 return m_pending_macros
;
129 /* Maintain the lists of symbols and blocks. */
131 /* Add a symbol to one of the lists of symbols. */
134 add_symbol_to_list (struct symbol
*symbol
, struct pending
**listhead
)
136 struct pending
*link
;
138 /* If this is an alias for another symbol, don't add it. */
139 if (symbol
->name
&& symbol
->name
[0] == '#')
142 /* We keep PENDINGSIZE symbols in each link of the list. If we
143 don't have a link with room in it, add a new link. */
144 if (*listhead
== NULL
|| (*listhead
)->nsyms
== PENDINGSIZE
)
146 link
= XNEW (struct pending
);
147 link
->next
= *listhead
;
152 (*listhead
)->symbol
[(*listhead
)->nsyms
++] = symbol
;
155 /* Find a symbol named NAME on a LIST. NAME need not be
156 '\0'-terminated; LENGTH is the length of the name. */
159 find_symbol_in_list (struct pending
*list
, char *name
, int length
)
166 for (j
= list
->nsyms
; --j
>= 0;)
168 pp
= list
->symbol
[j
]->linkage_name ();
169 if (*pp
== *name
&& strncmp (pp
, name
, length
) == 0
170 && pp
[length
] == '\0')
172 return (list
->symbol
[j
]);
180 /* Record BLOCK on the list of all blocks in the file. Put it after
181 OPBLOCK, or at the beginning if opblock is NULL. This puts the
182 block in the list after all its subblocks. */
185 buildsym_compunit::record_pending_block (struct block
*block
,
186 struct pending_block
*opblock
)
188 struct pending_block
*pblock
;
190 pblock
= XOBNEW (&m_pending_block_obstack
, struct pending_block
);
191 pblock
->block
= block
;
194 pblock
->next
= opblock
->next
;
195 opblock
->next
= pblock
;
199 pblock
->next
= m_pending_blocks
;
200 m_pending_blocks
= pblock
;
204 /* Take one of the lists of symbols and make a block from it. Keep
205 the order the symbols have in the list (reversed from the input
206 file). Put the block on the list of pending blocks. */
209 buildsym_compunit::finish_block_internal
210 (struct symbol
*symbol
,
211 struct pending
**listhead
,
212 struct pending_block
*old_blocks
,
213 const struct dynamic_prop
*static_link
,
214 CORE_ADDR start
, CORE_ADDR end
,
215 int is_global
, int expandable
)
217 struct gdbarch
*gdbarch
= get_objfile_arch (m_objfile
);
218 struct pending
*next
, *next1
;
220 struct pending_block
*pblock
;
221 struct pending_block
*opblock
;
224 ? allocate_global_block (&m_objfile
->objfile_obstack
)
225 : allocate_block (&m_objfile
->objfile_obstack
));
229 BLOCK_MULTIDICT (block
)
230 = mdict_create_linear (&m_objfile
->objfile_obstack
, *listhead
);
236 BLOCK_MULTIDICT (block
) = mdict_create_hashed_expandable (m_language
);
237 mdict_add_pending (BLOCK_MULTIDICT (block
), *listhead
);
241 BLOCK_MULTIDICT (block
) =
242 mdict_create_hashed (&m_objfile
->objfile_obstack
, *listhead
);
246 BLOCK_START (block
) = start
;
247 BLOCK_END (block
) = end
;
249 /* Put the block in as the value of the symbol that names it. */
253 struct type
*ftype
= SYMBOL_TYPE (symbol
);
254 struct mdict_iterator miter
;
255 SYMBOL_BLOCK_VALUE (symbol
) = block
;
256 BLOCK_FUNCTION (block
) = symbol
;
258 if (TYPE_NFIELDS (ftype
) <= 0)
260 /* No parameter type information is recorded with the
261 function's type. Set that from the type of the
262 parameter symbols. */
263 int nparams
= 0, iparams
;
266 /* Here we want to directly access the dictionary, because
267 we haven't fully initialized the block yet. */
268 ALL_DICT_SYMBOLS (BLOCK_MULTIDICT (block
), miter
, sym
)
270 if (SYMBOL_IS_ARGUMENT (sym
))
275 TYPE_NFIELDS (ftype
) = nparams
;
276 TYPE_FIELDS (ftype
) = (struct field
*)
277 TYPE_ALLOC (ftype
, nparams
* sizeof (struct field
));
280 /* Here we want to directly access the dictionary, because
281 we haven't fully initialized the block yet. */
282 ALL_DICT_SYMBOLS (BLOCK_MULTIDICT (block
), miter
, sym
)
284 if (iparams
== nparams
)
287 if (SYMBOL_IS_ARGUMENT (sym
))
289 TYPE_FIELD_TYPE (ftype
, iparams
) = SYMBOL_TYPE (sym
);
290 TYPE_FIELD_ARTIFICIAL (ftype
, iparams
) = 0;
299 BLOCK_FUNCTION (block
) = NULL
;
302 if (static_link
!= NULL
)
303 objfile_register_static_link (m_objfile
, block
, static_link
);
305 /* Now free the links of the list, and empty the list. */
307 for (next
= *listhead
; next
; next
= next1
)
314 /* Check to be sure that the blocks have an end address that is
315 greater than starting address. */
317 if (BLOCK_END (block
) < BLOCK_START (block
))
321 complaint (_("block end address less than block "
322 "start address in %s (patched it)"),
323 symbol
->print_name ());
327 complaint (_("block end address %s less than block "
328 "start address %s (patched it)"),
329 paddress (gdbarch
, BLOCK_END (block
)),
330 paddress (gdbarch
, BLOCK_START (block
)));
332 /* Better than nothing. */
333 BLOCK_END (block
) = BLOCK_START (block
);
336 /* Install this block as the superblock of all blocks made since the
337 start of this scope that don't have superblocks yet. */
340 for (pblock
= m_pending_blocks
;
341 pblock
&& pblock
!= old_blocks
;
342 pblock
= pblock
->next
)
344 if (BLOCK_SUPERBLOCK (pblock
->block
) == NULL
)
346 /* Check to be sure the blocks are nested as we receive
347 them. If the compiler/assembler/linker work, this just
348 burns a small amount of time.
350 Skip blocks which correspond to a function; they're not
351 physically nested inside this other blocks, only
353 if (BLOCK_FUNCTION (pblock
->block
) == NULL
354 && (BLOCK_START (pblock
->block
) < BLOCK_START (block
)
355 || BLOCK_END (pblock
->block
) > BLOCK_END (block
)))
359 complaint (_("inner block not inside outer block in %s"),
360 symbol
->print_name ());
364 complaint (_("inner block (%s-%s) not "
365 "inside outer block (%s-%s)"),
366 paddress (gdbarch
, BLOCK_START (pblock
->block
)),
367 paddress (gdbarch
, BLOCK_END (pblock
->block
)),
368 paddress (gdbarch
, BLOCK_START (block
)),
369 paddress (gdbarch
, BLOCK_END (block
)));
371 if (BLOCK_START (pblock
->block
) < BLOCK_START (block
))
372 BLOCK_START (pblock
->block
) = BLOCK_START (block
);
373 if (BLOCK_END (pblock
->block
) > BLOCK_END (block
))
374 BLOCK_END (pblock
->block
) = BLOCK_END (block
);
376 BLOCK_SUPERBLOCK (pblock
->block
) = block
;
381 block_set_using (block
,
383 ? m_global_using_directives
384 : m_local_using_directives
),
385 &m_objfile
->objfile_obstack
);
387 m_global_using_directives
= NULL
;
389 m_local_using_directives
= NULL
;
391 record_pending_block (block
, opblock
);
397 buildsym_compunit::finish_block (struct symbol
*symbol
,
398 struct pending_block
*old_blocks
,
399 const struct dynamic_prop
*static_link
,
400 CORE_ADDR start
, CORE_ADDR end
)
402 return finish_block_internal (symbol
, &m_local_symbols
,
403 old_blocks
, static_link
, start
, end
, 0, 0);
406 /* Record that the range of addresses from START to END_INCLUSIVE
407 (inclusive, like it says) belongs to BLOCK. BLOCK's start and end
408 addresses must be set already. You must apply this function to all
409 BLOCK's children before applying it to BLOCK.
411 If a call to this function complicates the picture beyond that
412 already provided by BLOCK_START and BLOCK_END, then we create an
413 address map for the block. */
415 buildsym_compunit::record_block_range (struct block
*block
,
417 CORE_ADDR end_inclusive
)
419 /* If this is any different from the range recorded in the block's
420 own BLOCK_START and BLOCK_END, then note that the address map has
421 become interesting. Note that even if this block doesn't have
422 any "interesting" ranges, some later block might, so we still
423 need to record this block in the addrmap. */
424 if (start
!= BLOCK_START (block
)
425 || end_inclusive
+ 1 != BLOCK_END (block
))
426 m_pending_addrmap_interesting
= true;
428 if (m_pending_addrmap
== nullptr)
429 m_pending_addrmap
= addrmap_create_mutable (&m_pending_addrmap_obstack
);
431 addrmap_set_empty (m_pending_addrmap
, start
, end_inclusive
, block
);
435 buildsym_compunit::make_blockvector ()
437 struct pending_block
*next
;
438 struct blockvector
*blockvector
;
441 /* Count the length of the list of blocks. */
443 for (next
= m_pending_blocks
, i
= 0; next
; next
= next
->next
, i
++)
447 blockvector
= (struct blockvector
*)
448 obstack_alloc (&m_objfile
->objfile_obstack
,
449 (sizeof (struct blockvector
)
450 + (i
- 1) * sizeof (struct block
*)));
452 /* Copy the blocks into the blockvector. This is done in reverse
453 order, which happens to put the blocks into the proper order
454 (ascending starting address). finish_block has hair to insert
455 each block into the list after its subblocks in order to make
456 sure this is true. */
458 BLOCKVECTOR_NBLOCKS (blockvector
) = i
;
459 for (next
= m_pending_blocks
; next
; next
= next
->next
)
461 BLOCKVECTOR_BLOCK (blockvector
, --i
) = next
->block
;
464 free_pending_blocks ();
466 /* If we needed an address map for this symtab, record it in the
468 if (m_pending_addrmap
!= nullptr && m_pending_addrmap_interesting
)
469 BLOCKVECTOR_MAP (blockvector
)
470 = addrmap_create_fixed (m_pending_addrmap
, &m_objfile
->objfile_obstack
);
472 BLOCKVECTOR_MAP (blockvector
) = 0;
474 /* Some compilers output blocks in the wrong order, but we depend on
475 their being in the right order so we can binary search. Check the
476 order and moan about it.
477 Note: Remember that the first two blocks are the global and static
478 blocks. We could special case that fact and begin checking at block 2.
479 To avoid making that assumption we do not. */
480 if (BLOCKVECTOR_NBLOCKS (blockvector
) > 1)
482 for (i
= 1; i
< BLOCKVECTOR_NBLOCKS (blockvector
); i
++)
484 if (BLOCK_START (BLOCKVECTOR_BLOCK (blockvector
, i
- 1))
485 > BLOCK_START (BLOCKVECTOR_BLOCK (blockvector
, i
)))
488 = BLOCK_START (BLOCKVECTOR_BLOCK (blockvector
, i
));
490 complaint (_("block at %s out of order"),
491 hex_string ((LONGEST
) start
));
496 return (blockvector
);
499 /* Start recording information about source code that came from an
500 included (or otherwise merged-in) source file with a different
501 name. NAME is the name of the file (cannot be NULL). */
504 buildsym_compunit::start_subfile (const char *name
)
506 const char *subfile_dirname
;
507 struct subfile
*subfile
;
509 subfile_dirname
= m_comp_dir
.get ();
511 /* See if this subfile is already registered. */
513 for (subfile
= m_subfiles
; subfile
; subfile
= subfile
->next
)
517 /* If NAME is an absolute path, and this subfile is not, then
518 attempt to create an absolute path to compare. */
519 if (IS_ABSOLUTE_PATH (name
)
520 && !IS_ABSOLUTE_PATH (subfile
->name
)
521 && subfile_dirname
!= NULL
)
522 subfile_name
= concat (subfile_dirname
, SLASH_STRING
,
523 subfile
->name
, (char *) NULL
);
525 subfile_name
= subfile
->name
;
527 if (FILENAME_CMP (subfile_name
, name
) == 0)
529 m_current_subfile
= subfile
;
530 if (subfile_name
!= subfile
->name
)
531 xfree (subfile_name
);
534 if (subfile_name
!= subfile
->name
)
535 xfree (subfile_name
);
538 /* This subfile is not known. Add an entry for it. */
540 subfile
= XNEW (struct subfile
);
541 memset (subfile
, 0, sizeof (struct subfile
));
542 subfile
->buildsym_compunit
= this;
544 subfile
->next
= m_subfiles
;
545 m_subfiles
= subfile
;
547 m_current_subfile
= subfile
;
549 subfile
->name
= xstrdup (name
);
551 /* Initialize line-number recording for this subfile. */
552 subfile
->line_vector
= NULL
;
554 /* Default the source language to whatever can be deduced from the
555 filename. If nothing can be deduced (such as for a C/C++ include
556 file with a ".h" extension), then inherit whatever language the
557 previous subfile had. This kludgery is necessary because there
558 is no standard way in some object formats to record the source
559 language. Also, when symtabs are allocated we try to deduce a
560 language then as well, but it is too late for us to use that
561 information while reading symbols, since symtabs aren't allocated
562 until after all the symbols have been processed for a given
565 subfile
->language
= deduce_language_from_filename (subfile
->name
);
566 if (subfile
->language
== language_unknown
567 && subfile
->next
!= NULL
)
569 subfile
->language
= subfile
->next
->language
;
572 /* If the filename of this subfile ends in .C, then change the
573 language of any pending subfiles from C to C++. We also accept
574 any other C++ suffixes accepted by deduce_language_from_filename. */
575 /* Likewise for f2c. */
580 enum language sublang
= deduce_language_from_filename (subfile
->name
);
582 if (sublang
== language_cplus
|| sublang
== language_fortran
)
583 for (s
= m_subfiles
; s
!= NULL
; s
= s
->next
)
584 if (s
->language
== language_c
)
585 s
->language
= sublang
;
588 /* And patch up this file if necessary. */
589 if (subfile
->language
== language_c
590 && subfile
->next
!= NULL
591 && (subfile
->next
->language
== language_cplus
592 || subfile
->next
->language
== language_fortran
))
594 subfile
->language
= subfile
->next
->language
;
598 /* For stabs readers, the first N_SO symbol is assumed to be the
599 source file name, and the subfile struct is initialized using that
600 assumption. If another N_SO symbol is later seen, immediately
601 following the first one, then the first one is assumed to be the
602 directory name and the second one is really the source file name.
604 So we have to patch up the subfile struct by moving the old name
605 value to dirname and remembering the new name. Some sanity
606 checking is performed to ensure that the state of the subfile
607 struct is reasonable and that the old name we are assuming to be a
608 directory name actually is (by checking for a trailing '/'). */
611 buildsym_compunit::patch_subfile_names (struct subfile
*subfile
,
615 && m_comp_dir
== NULL
616 && subfile
->name
!= NULL
617 && IS_DIR_SEPARATOR (subfile
->name
[strlen (subfile
->name
) - 1]))
619 m_comp_dir
.reset (subfile
->name
);
620 subfile
->name
= xstrdup (name
);
621 set_last_source_file (name
);
623 /* Default the source language to whatever can be deduced from
624 the filename. If nothing can be deduced (such as for a C/C++
625 include file with a ".h" extension), then inherit whatever
626 language the previous subfile had. This kludgery is
627 necessary because there is no standard way in some object
628 formats to record the source language. Also, when symtabs
629 are allocated we try to deduce a language then as well, but
630 it is too late for us to use that information while reading
631 symbols, since symtabs aren't allocated until after all the
632 symbols have been processed for a given source file. */
634 subfile
->language
= deduce_language_from_filename (subfile
->name
);
635 if (subfile
->language
== language_unknown
636 && subfile
->next
!= NULL
)
638 subfile
->language
= subfile
->next
->language
;
643 /* Handle the N_BINCL and N_EINCL symbol types that act like N_SOL for
644 switching source files (different subfiles, as we call them) within
645 one object file, but using a stack rather than in an arbitrary
649 buildsym_compunit::push_subfile ()
651 gdb_assert (m_current_subfile
!= NULL
);
652 gdb_assert (m_current_subfile
->name
!= NULL
);
653 m_subfile_stack
.push_back (m_current_subfile
->name
);
657 buildsym_compunit::pop_subfile ()
659 gdb_assert (!m_subfile_stack
.empty ());
660 const char *name
= m_subfile_stack
.back ();
661 m_subfile_stack
.pop_back ();
665 /* Add a linetable entry for line number LINE and address PC to the
666 line vector for SUBFILE. */
669 buildsym_compunit::record_line (struct subfile
*subfile
, int line
,
672 struct linetable_entry
*e
;
674 /* Ignore the dummy line number in libg.o */
680 /* Make sure line vector exists and is big enough. */
681 if (!subfile
->line_vector
)
683 subfile
->line_vector_length
= INITIAL_LINE_VECTOR_LENGTH
;
684 subfile
->line_vector
= (struct linetable
*)
685 xmalloc (sizeof (struct linetable
)
686 + subfile
->line_vector_length
* sizeof (struct linetable_entry
));
687 subfile
->line_vector
->nitems
= 0;
688 m_have_line_numbers
= true;
691 if (subfile
->line_vector
->nitems
+ 1 >= subfile
->line_vector_length
)
693 subfile
->line_vector_length
*= 2;
694 subfile
->line_vector
= (struct linetable
*)
695 xrealloc ((char *) subfile
->line_vector
,
696 (sizeof (struct linetable
)
697 + (subfile
->line_vector_length
698 * sizeof (struct linetable_entry
))));
701 /* Normally, we treat lines as unsorted. But the end of sequence
702 marker is special. We sort line markers at the same PC by line
703 number, so end of sequence markers (which have line == 0) appear
704 first. This is right if the marker ends the previous function,
705 and there is no padding before the next function. But it is
706 wrong if the previous line was empty and we are now marking a
707 switch to a different subfile. We must leave the end of sequence
708 marker at the end of this group of lines, not sort the empty line
709 to after the marker. The easiest way to accomplish this is to
710 delete any empty lines from our table, if they are followed by
711 end of sequence markers. All we lose is the ability to set
712 breakpoints at some lines which contain no instructions
714 if (line
== 0 && subfile
->line_vector
->nitems
> 0)
716 e
= subfile
->line_vector
->item
+ subfile
->line_vector
->nitems
- 1;
717 while (subfile
->line_vector
->nitems
> 0 && e
->pc
== pc
)
720 subfile
->line_vector
->nitems
--;
724 e
= subfile
->line_vector
->item
+ subfile
->line_vector
->nitems
++;
729 /* Needed in order to sort line tables from IBM xcoff files. Sigh! */
732 lte_is_less_than (const linetable_entry
&ln1
, const linetable_entry
&ln2
)
734 /* Note: this code does not assume that CORE_ADDRs can fit in ints.
735 Please keep it that way. */
742 /* If pc equal, sort by line. I'm not sure whether this is optimum
743 behavior (see comment at struct linetable in symtab.h). */
744 return ln1
.line
< ln2
.line
;
747 /* Subroutine of end_symtab to simplify it. Look for a subfile that
748 matches the main source file's basename. If there is only one, and
749 if the main source file doesn't have any symbol or line number
750 information, then copy this file's symtab and line_vector to the
751 main source file's subfile and discard the other subfile. This can
752 happen because of a compiler bug or from the user playing games
753 with #line or from things like a distributed build system that
754 manipulates the debug info. This can also happen from an innocent
755 symlink in the paths, we don't canonicalize paths here. */
758 buildsym_compunit::watch_main_source_file_lossage ()
760 struct subfile
*mainsub
, *subfile
;
762 /* Get the main source file. */
763 mainsub
= m_main_subfile
;
765 /* If the main source file doesn't have any line number or symbol
766 info, look for an alias in another subfile. */
768 if (mainsub
->line_vector
== NULL
769 && mainsub
->symtab
== NULL
)
771 const char *mainbase
= lbasename (mainsub
->name
);
773 struct subfile
*prevsub
;
774 struct subfile
*mainsub_alias
= NULL
;
775 struct subfile
*prev_mainsub_alias
= NULL
;
778 for (subfile
= m_subfiles
;
780 subfile
= subfile
->next
)
782 if (subfile
== mainsub
)
784 if (filename_cmp (lbasename (subfile
->name
), mainbase
) == 0)
787 mainsub_alias
= subfile
;
788 prev_mainsub_alias
= prevsub
;
795 gdb_assert (mainsub_alias
!= NULL
&& mainsub_alias
!= mainsub
);
797 /* Found a match for the main source file.
798 Copy its line_vector and symtab to the main subfile
799 and then discard it. */
801 mainsub
->line_vector
= mainsub_alias
->line_vector
;
802 mainsub
->line_vector_length
= mainsub_alias
->line_vector_length
;
803 mainsub
->symtab
= mainsub_alias
->symtab
;
805 if (prev_mainsub_alias
== NULL
)
806 m_subfiles
= mainsub_alias
->next
;
808 prev_mainsub_alias
->next
= mainsub_alias
->next
;
809 xfree (mainsub_alias
->name
);
810 xfree (mainsub_alias
);
815 /* Implementation of the first part of end_symtab. It allows modifying
816 STATIC_BLOCK before it gets finalized by end_symtab_from_static_block.
817 If the returned value is NULL there is no blockvector created for
818 this symtab (you still must call end_symtab_from_static_block).
820 END_ADDR is the same as for end_symtab: the address of the end of the
823 If EXPANDABLE is non-zero the STATIC_BLOCK dictionary is made
826 If REQUIRED is non-zero, then a symtab is created even if it does
827 not contain any symbols. */
830 buildsym_compunit::end_symtab_get_static_block (CORE_ADDR end_addr
,
831 int expandable
, int required
)
833 /* Finish the lexical context of the last function in the file; pop
834 the context stack. */
836 if (!m_context_stack
.empty ())
838 struct context_stack cstk
= pop_context ();
840 /* Make a block for the local symbols within. */
841 finish_block (cstk
.name
, cstk
.old_blocks
, NULL
,
842 cstk
.start_addr
, end_addr
);
844 if (!m_context_stack
.empty ())
846 /* This is said to happen with SCO. The old coffread.c
847 code simply emptied the context stack, so we do the
848 same. FIXME: Find out why it is happening. This is not
849 believed to happen in most cases (even for coffread.c);
850 it used to be an abort(). */
851 complaint (_("Context stack not empty in end_symtab"));
852 m_context_stack
.clear ();
856 /* Reordered executables may have out of order pending blocks; if
857 OBJF_REORDERED is true, then sort the pending blocks. */
859 if ((m_objfile
->flags
& OBJF_REORDERED
) && m_pending_blocks
)
861 struct pending_block
*pb
;
863 std::vector
<block
*> barray
;
865 for (pb
= m_pending_blocks
; pb
!= NULL
; pb
= pb
->next
)
866 barray
.push_back (pb
->block
);
868 /* Sort blocks by start address in descending order. Blocks with the
869 same start address must remain in the original order to preserve
870 inline function caller/callee relationships. */
871 std::stable_sort (barray
.begin (), barray
.end (),
872 [] (const block
*a
, const block
*b
)
874 return BLOCK_START (a
) > BLOCK_START (b
);
878 for (pb
= m_pending_blocks
; pb
!= NULL
; pb
= pb
->next
)
879 pb
->block
= barray
[i
++];
882 /* Cleanup any undefined types that have been left hanging around
883 (this needs to be done before the finish_blocks so that
884 file_symbols is still good).
886 Both cleanup_undefined_stabs_types and finish_global_stabs are stabs
887 specific, but harmless for other symbol readers, since on gdb
888 startup or when finished reading stabs, the state is set so these
889 are no-ops. FIXME: Is this handled right in case of QUIT? Can
890 we make this cleaner? */
892 cleanup_undefined_stabs_types (m_objfile
);
893 finish_global_stabs (m_objfile
);
896 && m_pending_blocks
== NULL
897 && m_file_symbols
== NULL
898 && m_global_symbols
== NULL
899 && !m_have_line_numbers
900 && m_pending_macros
== NULL
901 && m_global_using_directives
== NULL
)
903 /* Ignore symtabs that have no functions with real debugging info. */
908 /* Define the STATIC_BLOCK. */
909 return finish_block_internal (NULL
, get_file_symbols (), NULL
, NULL
,
910 m_last_source_start_addr
,
911 end_addr
, 0, expandable
);
915 /* Subroutine of end_symtab_from_static_block to simplify it.
916 Handle the "have blockvector" case.
917 See end_symtab_from_static_block for a description of the arguments. */
919 struct compunit_symtab
*
920 buildsym_compunit::end_symtab_with_blockvector (struct block
*static_block
,
921 int section
, int expandable
)
923 struct compunit_symtab
*cu
= m_compunit_symtab
;
924 struct blockvector
*blockvector
;
925 struct subfile
*subfile
;
928 gdb_assert (static_block
!= NULL
);
929 gdb_assert (m_subfiles
!= NULL
);
931 end_addr
= BLOCK_END (static_block
);
933 /* Create the GLOBAL_BLOCK and build the blockvector. */
934 finish_block_internal (NULL
, get_global_symbols (), NULL
, NULL
,
935 m_last_source_start_addr
, end_addr
,
937 blockvector
= make_blockvector ();
939 /* Read the line table if it has to be read separately.
940 This is only used by xcoffread.c. */
941 if (m_objfile
->sf
->sym_read_linetable
!= NULL
)
942 m_objfile
->sf
->sym_read_linetable (m_objfile
);
944 /* Handle the case where the debug info specifies a different path
945 for the main source file. It can cause us to lose track of its
946 line number information. */
947 watch_main_source_file_lossage ();
949 /* Now create the symtab objects proper, if not already done,
950 one for each subfile. */
952 for (subfile
= m_subfiles
;
954 subfile
= subfile
->next
)
956 int linetablesize
= 0;
958 if (subfile
->line_vector
)
960 linetablesize
= sizeof (struct linetable
) +
961 subfile
->line_vector
->nitems
* sizeof (struct linetable_entry
);
963 /* Like the pending blocks, the line table may be
964 scrambled in reordered executables. Sort it if
965 OBJF_REORDERED is true. */
966 if (m_objfile
->flags
& OBJF_REORDERED
)
967 std::sort (subfile
->line_vector
->item
,
968 subfile
->line_vector
->item
969 + subfile
->line_vector
->nitems
,
973 /* Allocate a symbol table if necessary. */
974 if (subfile
->symtab
== NULL
)
975 subfile
->symtab
= allocate_symtab (cu
, subfile
->name
);
976 struct symtab
*symtab
= subfile
->symtab
;
978 /* Fill in its components. */
980 if (subfile
->line_vector
)
982 /* Reallocate the line table on the symbol obstack. */
983 SYMTAB_LINETABLE (symtab
) = (struct linetable
*)
984 obstack_alloc (&m_objfile
->objfile_obstack
, linetablesize
);
985 memcpy (SYMTAB_LINETABLE (symtab
), subfile
->line_vector
,
990 SYMTAB_LINETABLE (symtab
) = NULL
;
993 /* Use whatever language we have been using for this
994 subfile, not the one that was deduced in allocate_symtab
995 from the filename. We already did our own deducing when
996 we created the subfile, and we may have altered our
997 opinion of what language it is from things we found in
999 symtab
->language
= subfile
->language
;
1002 /* Make sure the symtab of main_subfile is the first in its list. */
1004 struct symtab
*main_symtab
, *prev_symtab
;
1006 main_symtab
= m_main_subfile
->symtab
;
1008 for (symtab
*symtab
: compunit_filetabs (cu
))
1010 if (symtab
== main_symtab
)
1012 if (prev_symtab
!= NULL
)
1014 prev_symtab
->next
= main_symtab
->next
;
1015 main_symtab
->next
= COMPUNIT_FILETABS (cu
);
1016 COMPUNIT_FILETABS (cu
) = main_symtab
;
1020 prev_symtab
= symtab
;
1022 gdb_assert (main_symtab
== COMPUNIT_FILETABS (cu
));
1025 /* Fill out the compunit symtab. */
1027 if (m_comp_dir
!= NULL
)
1029 /* Reallocate the dirname on the symbol obstack. */
1030 const char *comp_dir
= m_comp_dir
.get ();
1031 COMPUNIT_DIRNAME (cu
) = obstack_strdup (&m_objfile
->objfile_obstack
,
1035 /* Save the debug format string (if any) in the symtab. */
1036 COMPUNIT_DEBUGFORMAT (cu
) = obstack_strdup (&m_objfile
->objfile_obstack
,
1037 m_debugformat
.get ());
1039 /* Similarly for the producer. */
1040 COMPUNIT_PRODUCER (cu
) = m_producer
;
1042 COMPUNIT_BLOCKVECTOR (cu
) = blockvector
;
1044 struct block
*b
= BLOCKVECTOR_BLOCK (blockvector
, GLOBAL_BLOCK
);
1046 set_block_compunit_symtab (b
, cu
);
1049 COMPUNIT_BLOCK_LINE_SECTION (cu
) = section
;
1051 COMPUNIT_MACRO_TABLE (cu
) = release_macros ();
1053 /* Default any symbols without a specified symtab to the primary symtab. */
1057 /* The main source file's symtab. */
1058 struct symtab
*symtab
= COMPUNIT_FILETABS (cu
);
1060 for (block_i
= 0; block_i
< BLOCKVECTOR_NBLOCKS (blockvector
); block_i
++)
1062 struct block
*block
= BLOCKVECTOR_BLOCK (blockvector
, block_i
);
1064 struct mdict_iterator miter
;
1066 /* Inlined functions may have symbols not in the global or
1067 static symbol lists. */
1068 if (BLOCK_FUNCTION (block
) != NULL
)
1069 if (symbol_symtab (BLOCK_FUNCTION (block
)) == NULL
)
1070 symbol_set_symtab (BLOCK_FUNCTION (block
), symtab
);
1072 /* Note that we only want to fix up symbols from the local
1073 blocks, not blocks coming from included symtabs. That is why
1074 we use ALL_DICT_SYMBOLS here and not ALL_BLOCK_SYMBOLS. */
1075 ALL_DICT_SYMBOLS (BLOCK_MULTIDICT (block
), miter
, sym
)
1076 if (symbol_symtab (sym
) == NULL
)
1077 symbol_set_symtab (sym
, symtab
);
1081 add_compunit_symtab_to_objfile (cu
);
1086 /* Implementation of the second part of end_symtab. Pass STATIC_BLOCK
1087 as value returned by end_symtab_get_static_block.
1089 SECTION is the same as for end_symtab: the section number
1090 (in objfile->section_offsets) of the blockvector and linetable.
1092 If EXPANDABLE is non-zero the GLOBAL_BLOCK dictionary is made
1095 struct compunit_symtab
*
1096 buildsym_compunit::end_symtab_from_static_block (struct block
*static_block
,
1097 int section
, int expandable
)
1099 struct compunit_symtab
*cu
;
1101 if (static_block
== NULL
)
1103 /* Handle the "no blockvector" case.
1104 When this happens there is nothing to record, so there's nothing
1105 to do: memory will be freed up later.
1107 Note: We won't be adding a compunit to the objfile's list of
1108 compunits, so there's nothing to unchain. However, since each symtab
1109 is added to the objfile's obstack we can't free that space.
1110 We could do better, but this is believed to be a sufficiently rare
1115 cu
= end_symtab_with_blockvector (static_block
, section
, expandable
);
1120 /* Finish the symbol definitions for one main source file, close off
1121 all the lexical contexts for that file (creating struct block's for
1122 them), then make the struct symtab for that file and put it in the
1125 END_ADDR is the address of the end of the file's text. SECTION is
1126 the section number (in objfile->section_offsets) of the blockvector
1129 Note that it is possible for end_symtab() to return NULL. In
1130 particular, for the DWARF case at least, it will return NULL when
1131 it finds a compilation unit that has exactly one DIE, a
1132 TAG_compile_unit DIE. This can happen when we link in an object
1133 file that was compiled from an empty source file. Returning NULL
1134 is probably not the correct thing to do, because then gdb will
1135 never know about this empty file (FIXME).
1137 If you need to modify STATIC_BLOCK before it is finalized you should
1138 call end_symtab_get_static_block and end_symtab_from_static_block
1141 struct compunit_symtab
*
1142 buildsym_compunit::end_symtab (CORE_ADDR end_addr
, int section
)
1144 struct block
*static_block
;
1146 static_block
= end_symtab_get_static_block (end_addr
, 0, 0);
1147 return end_symtab_from_static_block (static_block
, section
, 0);
1150 /* Same as end_symtab except create a symtab that can be later added to. */
1152 struct compunit_symtab
*
1153 buildsym_compunit::end_expandable_symtab (CORE_ADDR end_addr
, int section
)
1155 struct block
*static_block
;
1157 static_block
= end_symtab_get_static_block (end_addr
, 1, 0);
1158 return end_symtab_from_static_block (static_block
, section
, 1);
1161 /* Subroutine of augment_type_symtab to simplify it.
1162 Attach the main source file's symtab to all symbols in PENDING_LIST that
1166 set_missing_symtab (struct pending
*pending_list
,
1167 struct compunit_symtab
*cu
)
1169 struct pending
*pending
;
1172 for (pending
= pending_list
; pending
!= NULL
; pending
= pending
->next
)
1174 for (i
= 0; i
< pending
->nsyms
; ++i
)
1176 if (symbol_symtab (pending
->symbol
[i
]) == NULL
)
1177 symbol_set_symtab (pending
->symbol
[i
], COMPUNIT_FILETABS (cu
));
1182 /* Same as end_symtab, but for the case where we're adding more symbols
1183 to an existing symtab that is known to contain only type information.
1184 This is the case for DWARF4 Type Units. */
1187 buildsym_compunit::augment_type_symtab ()
1189 struct compunit_symtab
*cust
= m_compunit_symtab
;
1190 const struct blockvector
*blockvector
= COMPUNIT_BLOCKVECTOR (cust
);
1192 if (!m_context_stack
.empty ())
1193 complaint (_("Context stack not empty in augment_type_symtab"));
1194 if (m_pending_blocks
!= NULL
)
1195 complaint (_("Blocks in a type symtab"));
1196 if (m_pending_macros
!= NULL
)
1197 complaint (_("Macro in a type symtab"));
1198 if (m_have_line_numbers
)
1199 complaint (_("Line numbers recorded in a type symtab"));
1201 if (m_file_symbols
!= NULL
)
1203 struct block
*block
= BLOCKVECTOR_BLOCK (blockvector
, STATIC_BLOCK
);
1205 /* First mark any symbols without a specified symtab as belonging
1206 to the primary symtab. */
1207 set_missing_symtab (m_file_symbols
, cust
);
1209 mdict_add_pending (BLOCK_MULTIDICT (block
), m_file_symbols
);
1212 if (m_global_symbols
!= NULL
)
1214 struct block
*block
= BLOCKVECTOR_BLOCK (blockvector
, GLOBAL_BLOCK
);
1216 /* First mark any symbols without a specified symtab as belonging
1217 to the primary symtab. */
1218 set_missing_symtab (m_global_symbols
, cust
);
1220 mdict_add_pending (BLOCK_MULTIDICT (block
),
1225 /* Push a context block. Args are an identifying nesting level
1226 (checkable when you pop it), and the starting PC address of this
1229 struct context_stack
*
1230 buildsym_compunit::push_context (int desc
, CORE_ADDR valu
)
1232 m_context_stack
.emplace_back ();
1233 struct context_stack
*newobj
= &m_context_stack
.back ();
1235 newobj
->depth
= desc
;
1236 newobj
->locals
= m_local_symbols
;
1237 newobj
->old_blocks
= m_pending_blocks
;
1238 newobj
->start_addr
= valu
;
1239 newobj
->local_using_directives
= m_local_using_directives
;
1240 newobj
->name
= NULL
;
1242 m_local_symbols
= NULL
;
1243 m_local_using_directives
= NULL
;
1248 /* Pop a context block. Returns the address of the context block just
1251 struct context_stack
1252 buildsym_compunit::pop_context ()
1254 gdb_assert (!m_context_stack
.empty ());
1255 struct context_stack result
= m_context_stack
.back ();
1256 m_context_stack
.pop_back ();