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/>. */
20 #include "buildsym-legacy.h"
22 #include "gdb_obstack.h"
27 #include "complaints.h"
28 #include "expression.h" /* For "enum exp_opcode" used by... */
29 #include "filenames.h" /* For DOSish file names. */
31 #include "demangle.h" /* Needed by SYMBOL_INIT_DEMANGLED_NAME. */
33 #include "cp-support.h"
34 #include "dictionary.h"
38 /* For cleanup_undefined_stabs_types and finish_global_stabs (somewhat
39 questionable--see comment where we call them). */
41 #include "stabsread.h"
43 /* List of blocks already made (lexical contexts already closed).
44 This is used at the end to make the blockvector. */
48 struct pending_block
*next
;
52 /* Initial sizes of data structures. These are realloc'd larger if
53 needed, and realloc'd down to the size actually used, when
56 #define INITIAL_LINE_VECTOR_LENGTH 1000
59 buildsym_compunit::buildsym_compunit (struct objfile
*objfile_
,
61 const char *comp_dir_
,
62 enum language language_
,
64 : m_objfile (objfile_
),
65 m_last_source_file (name
== nullptr ? nullptr : xstrdup (name
)),
66 m_comp_dir (comp_dir_
== nullptr ? nullptr : xstrdup (comp_dir_
)),
67 m_language (language_
),
68 m_last_source_start_addr (last_addr
)
70 /* Allocate the compunit symtab now. The caller needs it to allocate
71 non-primary symtabs. It is also needed by get_macro_table. */
72 m_compunit_symtab
= allocate_compunit_symtab (m_objfile
, name
);
74 /* Build the subfile for NAME (the main source file) so that we can record
75 a pointer to it for later.
76 IMPORTANT: Do not allocate a struct symtab for NAME here.
77 It can happen that the debug info provides a different path to NAME than
78 DIRNAME,NAME. We cope with this in watch_main_source_file_lossage but
79 that only works if the main_subfile doesn't have a symtab yet. */
81 /* Save this so that we don't have to go looking for it at the end
82 of the subfiles list. */
83 m_main_subfile
= m_current_subfile
;
86 buildsym_compunit::~buildsym_compunit ()
88 struct subfile
*subfile
, *nextsub
;
90 if (m_pending_macros
!= nullptr)
91 free_macro_table (m_pending_macros
);
93 for (subfile
= m_subfiles
;
97 nextsub
= subfile
->next
;
98 xfree (subfile
->name
);
99 xfree (subfile
->line_vector
);
103 struct pending
*next
, *next1
;
105 for (next
= m_file_symbols
; next
!= NULL
; next
= next1
)
108 xfree ((void *) next
);
111 for (next
= m_global_symbols
; next
!= NULL
; next
= next1
)
114 xfree ((void *) next
);
119 buildsym_compunit::get_macro_table ()
121 if (m_pending_macros
== nullptr)
122 m_pending_macros
= new_macro_table (&m_objfile
->per_bfd
->storage_obstack
,
123 &m_objfile
->per_bfd
->macro_cache
,
125 return m_pending_macros
;
128 /* Maintain the lists of symbols and blocks. */
130 /* Add a symbol to one of the lists of symbols. */
133 add_symbol_to_list (struct symbol
*symbol
, struct pending
**listhead
)
135 struct pending
*link
;
137 /* If this is an alias for another symbol, don't add it. */
138 if (symbol
->name
&& symbol
->name
[0] == '#')
141 /* We keep PENDINGSIZE symbols in each link of the list. If we
142 don't have a link with room in it, add a new link. */
143 if (*listhead
== NULL
|| (*listhead
)->nsyms
== PENDINGSIZE
)
145 link
= XNEW (struct pending
);
146 link
->next
= *listhead
;
151 (*listhead
)->symbol
[(*listhead
)->nsyms
++] = symbol
;
154 /* Find a symbol named NAME on a LIST. NAME need not be
155 '\0'-terminated; LENGTH is the length of the name. */
158 find_symbol_in_list (struct pending
*list
, char *name
, int length
)
165 for (j
= list
->nsyms
; --j
>= 0;)
167 pp
= list
->symbol
[j
]->linkage_name ();
168 if (*pp
== *name
&& strncmp (pp
, name
, length
) == 0
169 && pp
[length
] == '\0')
171 return (list
->symbol
[j
]);
179 /* Record BLOCK on the list of all blocks in the file. Put it after
180 OPBLOCK, or at the beginning if opblock is NULL. This puts the
181 block in the list after all its subblocks. */
184 buildsym_compunit::record_pending_block (struct block
*block
,
185 struct pending_block
*opblock
)
187 struct pending_block
*pblock
;
189 pblock
= XOBNEW (&m_pending_block_obstack
, struct pending_block
);
190 pblock
->block
= block
;
193 pblock
->next
= opblock
->next
;
194 opblock
->next
= pblock
;
198 pblock
->next
= m_pending_blocks
;
199 m_pending_blocks
= pblock
;
203 /* Take one of the lists of symbols and make a block from it. Keep
204 the order the symbols have in the list (reversed from the input
205 file). Put the block on the list of pending blocks. */
208 buildsym_compunit::finish_block_internal
209 (struct symbol
*symbol
,
210 struct pending
**listhead
,
211 struct pending_block
*old_blocks
,
212 const struct dynamic_prop
*static_link
,
213 CORE_ADDR start
, CORE_ADDR end
,
214 int is_global
, int expandable
)
216 struct gdbarch
*gdbarch
= get_objfile_arch (m_objfile
);
217 struct pending
*next
, *next1
;
219 struct pending_block
*pblock
;
220 struct pending_block
*opblock
;
223 ? allocate_global_block (&m_objfile
->objfile_obstack
)
224 : allocate_block (&m_objfile
->objfile_obstack
));
228 BLOCK_MULTIDICT (block
)
229 = mdict_create_linear (&m_objfile
->objfile_obstack
, *listhead
);
235 BLOCK_MULTIDICT (block
) = mdict_create_hashed_expandable (m_language
);
236 mdict_add_pending (BLOCK_MULTIDICT (block
), *listhead
);
240 BLOCK_MULTIDICT (block
) =
241 mdict_create_hashed (&m_objfile
->objfile_obstack
, *listhead
);
245 BLOCK_START (block
) = start
;
246 BLOCK_END (block
) = end
;
248 /* Put the block in as the value of the symbol that names it. */
252 struct type
*ftype
= SYMBOL_TYPE (symbol
);
253 struct mdict_iterator miter
;
254 SYMBOL_BLOCK_VALUE (symbol
) = block
;
255 BLOCK_FUNCTION (block
) = symbol
;
257 if (TYPE_NFIELDS (ftype
) <= 0)
259 /* No parameter type information is recorded with the
260 function's type. Set that from the type of the
261 parameter symbols. */
262 int nparams
= 0, iparams
;
265 /* Here we want to directly access the dictionary, because
266 we haven't fully initialized the block yet. */
267 ALL_DICT_SYMBOLS (BLOCK_MULTIDICT (block
), miter
, sym
)
269 if (SYMBOL_IS_ARGUMENT (sym
))
274 TYPE_NFIELDS (ftype
) = nparams
;
275 TYPE_FIELDS (ftype
) = (struct field
*)
276 TYPE_ALLOC (ftype
, nparams
* sizeof (struct field
));
279 /* Here we want to directly access the dictionary, because
280 we haven't fully initialized the block yet. */
281 ALL_DICT_SYMBOLS (BLOCK_MULTIDICT (block
), miter
, sym
)
283 if (iparams
== nparams
)
286 if (SYMBOL_IS_ARGUMENT (sym
))
288 TYPE_FIELD_TYPE (ftype
, iparams
) = SYMBOL_TYPE (sym
);
289 TYPE_FIELD_ARTIFICIAL (ftype
, iparams
) = 0;
298 BLOCK_FUNCTION (block
) = NULL
;
301 if (static_link
!= NULL
)
302 objfile_register_static_link (m_objfile
, block
, static_link
);
304 /* Now free the links of the list, and empty the list. */
306 for (next
= *listhead
; next
; next
= next1
)
313 /* Check to be sure that the blocks have an end address that is
314 greater than starting address. */
316 if (BLOCK_END (block
) < BLOCK_START (block
))
320 complaint (_("block end address less than block "
321 "start address in %s (patched it)"),
322 symbol
->print_name ());
326 complaint (_("block end address %s less than block "
327 "start address %s (patched it)"),
328 paddress (gdbarch
, BLOCK_END (block
)),
329 paddress (gdbarch
, BLOCK_START (block
)));
331 /* Better than nothing. */
332 BLOCK_END (block
) = BLOCK_START (block
);
335 /* Install this block as the superblock of all blocks made since the
336 start of this scope that don't have superblocks yet. */
339 for (pblock
= m_pending_blocks
;
340 pblock
&& pblock
!= old_blocks
;
341 pblock
= pblock
->next
)
343 if (BLOCK_SUPERBLOCK (pblock
->block
) == NULL
)
345 /* Check to be sure the blocks are nested as we receive
346 them. If the compiler/assembler/linker work, this just
347 burns a small amount of time.
349 Skip blocks which correspond to a function; they're not
350 physically nested inside this other blocks, only
352 if (BLOCK_FUNCTION (pblock
->block
) == NULL
353 && (BLOCK_START (pblock
->block
) < BLOCK_START (block
)
354 || BLOCK_END (pblock
->block
) > BLOCK_END (block
)))
358 complaint (_("inner block not inside outer block in %s"),
359 symbol
->print_name ());
363 complaint (_("inner block (%s-%s) not "
364 "inside outer block (%s-%s)"),
365 paddress (gdbarch
, BLOCK_START (pblock
->block
)),
366 paddress (gdbarch
, BLOCK_END (pblock
->block
)),
367 paddress (gdbarch
, BLOCK_START (block
)),
368 paddress (gdbarch
, BLOCK_END (block
)));
370 if (BLOCK_START (pblock
->block
) < BLOCK_START (block
))
371 BLOCK_START (pblock
->block
) = BLOCK_START (block
);
372 if (BLOCK_END (pblock
->block
) > BLOCK_END (block
))
373 BLOCK_END (pblock
->block
) = BLOCK_END (block
);
375 BLOCK_SUPERBLOCK (pblock
->block
) = block
;
380 block_set_using (block
,
382 ? m_global_using_directives
383 : m_local_using_directives
),
384 &m_objfile
->objfile_obstack
);
386 m_global_using_directives
= NULL
;
388 m_local_using_directives
= NULL
;
390 record_pending_block (block
, opblock
);
396 buildsym_compunit::finish_block (struct symbol
*symbol
,
397 struct pending_block
*old_blocks
,
398 const struct dynamic_prop
*static_link
,
399 CORE_ADDR start
, CORE_ADDR end
)
401 return finish_block_internal (symbol
, &m_local_symbols
,
402 old_blocks
, static_link
, start
, end
, 0, 0);
405 /* Record that the range of addresses from START to END_INCLUSIVE
406 (inclusive, like it says) belongs to BLOCK. BLOCK's start and end
407 addresses must be set already. You must apply this function to all
408 BLOCK's children before applying it to BLOCK.
410 If a call to this function complicates the picture beyond that
411 already provided by BLOCK_START and BLOCK_END, then we create an
412 address map for the block. */
414 buildsym_compunit::record_block_range (struct block
*block
,
416 CORE_ADDR end_inclusive
)
418 /* If this is any different from the range recorded in the block's
419 own BLOCK_START and BLOCK_END, then note that the address map has
420 become interesting. Note that even if this block doesn't have
421 any "interesting" ranges, some later block might, so we still
422 need to record this block in the addrmap. */
423 if (start
!= BLOCK_START (block
)
424 || end_inclusive
+ 1 != BLOCK_END (block
))
425 m_pending_addrmap_interesting
= true;
427 if (m_pending_addrmap
== nullptr)
428 m_pending_addrmap
= addrmap_create_mutable (&m_pending_addrmap_obstack
);
430 addrmap_set_empty (m_pending_addrmap
, start
, end_inclusive
, block
);
434 buildsym_compunit::make_blockvector ()
436 struct pending_block
*next
;
437 struct blockvector
*blockvector
;
440 /* Count the length of the list of blocks. */
442 for (next
= m_pending_blocks
, i
= 0; next
; next
= next
->next
, i
++)
446 blockvector
= (struct blockvector
*)
447 obstack_alloc (&m_objfile
->objfile_obstack
,
448 (sizeof (struct blockvector
)
449 + (i
- 1) * sizeof (struct block
*)));
451 /* Copy the blocks into the blockvector. This is done in reverse
452 order, which happens to put the blocks into the proper order
453 (ascending starting address). finish_block has hair to insert
454 each block into the list after its subblocks in order to make
455 sure this is true. */
457 BLOCKVECTOR_NBLOCKS (blockvector
) = i
;
458 for (next
= m_pending_blocks
; next
; next
= next
->next
)
460 BLOCKVECTOR_BLOCK (blockvector
, --i
) = next
->block
;
463 free_pending_blocks ();
465 /* If we needed an address map for this symtab, record it in the
467 if (m_pending_addrmap
!= nullptr && m_pending_addrmap_interesting
)
468 BLOCKVECTOR_MAP (blockvector
)
469 = addrmap_create_fixed (m_pending_addrmap
, &m_objfile
->objfile_obstack
);
471 BLOCKVECTOR_MAP (blockvector
) = 0;
473 /* Some compilers output blocks in the wrong order, but we depend on
474 their being in the right order so we can binary search. Check the
475 order and moan about it.
476 Note: Remember that the first two blocks are the global and static
477 blocks. We could special case that fact and begin checking at block 2.
478 To avoid making that assumption we do not. */
479 if (BLOCKVECTOR_NBLOCKS (blockvector
) > 1)
481 for (i
= 1; i
< BLOCKVECTOR_NBLOCKS (blockvector
); i
++)
483 if (BLOCK_START (BLOCKVECTOR_BLOCK (blockvector
, i
- 1))
484 > BLOCK_START (BLOCKVECTOR_BLOCK (blockvector
, i
)))
487 = BLOCK_START (BLOCKVECTOR_BLOCK (blockvector
, i
));
489 complaint (_("block at %s out of order"),
490 hex_string ((LONGEST
) start
));
495 return (blockvector
);
498 /* Start recording information about source code that came from an
499 included (or otherwise merged-in) source file with a different
500 name. NAME is the name of the file (cannot be NULL). */
503 buildsym_compunit::start_subfile (const char *name
)
505 const char *subfile_dirname
;
506 struct subfile
*subfile
;
508 subfile_dirname
= m_comp_dir
.get ();
510 /* See if this subfile is already registered. */
512 for (subfile
= m_subfiles
; subfile
; subfile
= subfile
->next
)
516 /* If NAME is an absolute path, and this subfile is not, then
517 attempt to create an absolute path to compare. */
518 if (IS_ABSOLUTE_PATH (name
)
519 && !IS_ABSOLUTE_PATH (subfile
->name
)
520 && subfile_dirname
!= NULL
)
521 subfile_name
= concat (subfile_dirname
, SLASH_STRING
,
522 subfile
->name
, (char *) NULL
);
524 subfile_name
= subfile
->name
;
526 if (FILENAME_CMP (subfile_name
, name
) == 0)
528 m_current_subfile
= subfile
;
529 if (subfile_name
!= subfile
->name
)
530 xfree (subfile_name
);
533 if (subfile_name
!= subfile
->name
)
534 xfree (subfile_name
);
537 /* This subfile is not known. Add an entry for it. */
539 subfile
= XNEW (struct subfile
);
540 memset (subfile
, 0, sizeof (struct subfile
));
541 subfile
->buildsym_compunit
= this;
543 subfile
->next
= m_subfiles
;
544 m_subfiles
= subfile
;
546 m_current_subfile
= subfile
;
548 subfile
->name
= xstrdup (name
);
550 /* Initialize line-number recording for this subfile. */
551 subfile
->line_vector
= NULL
;
553 /* Default the source language to whatever can be deduced from the
554 filename. If nothing can be deduced (such as for a C/C++ include
555 file with a ".h" extension), then inherit whatever language the
556 previous subfile had. This kludgery is necessary because there
557 is no standard way in some object formats to record the source
558 language. Also, when symtabs are allocated we try to deduce a
559 language then as well, but it is too late for us to use that
560 information while reading symbols, since symtabs aren't allocated
561 until after all the symbols have been processed for a given
564 subfile
->language
= deduce_language_from_filename (subfile
->name
);
565 if (subfile
->language
== language_unknown
566 && subfile
->next
!= NULL
)
568 subfile
->language
= subfile
->next
->language
;
571 /* If the filename of this subfile ends in .C, then change the
572 language of any pending subfiles from C to C++. We also accept
573 any other C++ suffixes accepted by deduce_language_from_filename. */
574 /* Likewise for f2c. */
579 enum language sublang
= deduce_language_from_filename (subfile
->name
);
581 if (sublang
== language_cplus
|| sublang
== language_fortran
)
582 for (s
= m_subfiles
; s
!= NULL
; s
= s
->next
)
583 if (s
->language
== language_c
)
584 s
->language
= sublang
;
587 /* And patch up this file if necessary. */
588 if (subfile
->language
== language_c
589 && subfile
->next
!= NULL
590 && (subfile
->next
->language
== language_cplus
591 || subfile
->next
->language
== language_fortran
))
593 subfile
->language
= subfile
->next
->language
;
597 /* For stabs readers, the first N_SO symbol is assumed to be the
598 source file name, and the subfile struct is initialized using that
599 assumption. If another N_SO symbol is later seen, immediately
600 following the first one, then the first one is assumed to be the
601 directory name and the second one is really the source file name.
603 So we have to patch up the subfile struct by moving the old name
604 value to dirname and remembering the new name. Some sanity
605 checking is performed to ensure that the state of the subfile
606 struct is reasonable and that the old name we are assuming to be a
607 directory name actually is (by checking for a trailing '/'). */
610 buildsym_compunit::patch_subfile_names (struct subfile
*subfile
,
614 && m_comp_dir
== NULL
615 && subfile
->name
!= NULL
616 && IS_DIR_SEPARATOR (subfile
->name
[strlen (subfile
->name
) - 1]))
618 m_comp_dir
.reset (subfile
->name
);
619 subfile
->name
= xstrdup (name
);
620 set_last_source_file (name
);
622 /* Default the source language to whatever can be deduced from
623 the filename. If nothing can be deduced (such as for a C/C++
624 include file with a ".h" extension), then inherit whatever
625 language the previous subfile had. This kludgery is
626 necessary because there is no standard way in some object
627 formats to record the source language. Also, when symtabs
628 are allocated we try to deduce a language then as well, but
629 it is too late for us to use that information while reading
630 symbols, since symtabs aren't allocated until after all the
631 symbols have been processed for a given source file. */
633 subfile
->language
= deduce_language_from_filename (subfile
->name
);
634 if (subfile
->language
== language_unknown
635 && subfile
->next
!= NULL
)
637 subfile
->language
= subfile
->next
->language
;
642 /* Handle the N_BINCL and N_EINCL symbol types that act like N_SOL for
643 switching source files (different subfiles, as we call them) within
644 one object file, but using a stack rather than in an arbitrary
648 buildsym_compunit::push_subfile ()
650 gdb_assert (m_current_subfile
!= NULL
);
651 gdb_assert (m_current_subfile
->name
!= NULL
);
652 m_subfile_stack
.push_back (m_current_subfile
->name
);
656 buildsym_compunit::pop_subfile ()
658 gdb_assert (!m_subfile_stack
.empty ());
659 const char *name
= m_subfile_stack
.back ();
660 m_subfile_stack
.pop_back ();
664 /* Add a linetable entry for line number LINE and address PC to the
665 line vector for SUBFILE. */
668 buildsym_compunit::record_line (struct subfile
*subfile
, int line
,
671 struct linetable_entry
*e
;
673 /* Ignore the dummy line number in libg.o */
679 /* Make sure line vector exists and is big enough. */
680 if (!subfile
->line_vector
)
682 subfile
->line_vector_length
= INITIAL_LINE_VECTOR_LENGTH
;
683 subfile
->line_vector
= (struct linetable
*)
684 xmalloc (sizeof (struct linetable
)
685 + subfile
->line_vector_length
* sizeof (struct linetable_entry
));
686 subfile
->line_vector
->nitems
= 0;
687 m_have_line_numbers
= true;
690 if (subfile
->line_vector
->nitems
+ 1 >= subfile
->line_vector_length
)
692 subfile
->line_vector_length
*= 2;
693 subfile
->line_vector
= (struct linetable
*)
694 xrealloc ((char *) subfile
->line_vector
,
695 (sizeof (struct linetable
)
696 + (subfile
->line_vector_length
697 * sizeof (struct linetable_entry
))));
700 /* Normally, we treat lines as unsorted. But the end of sequence
701 marker is special. We sort line markers at the same PC by line
702 number, so end of sequence markers (which have line == 0) appear
703 first. This is right if the marker ends the previous function,
704 and there is no padding before the next function. But it is
705 wrong if the previous line was empty and we are now marking a
706 switch to a different subfile. We must leave the end of sequence
707 marker at the end of this group of lines, not sort the empty line
708 to after the marker. The easiest way to accomplish this is to
709 delete any empty lines from our table, if they are followed by
710 end of sequence markers. All we lose is the ability to set
711 breakpoints at some lines which contain no instructions
713 if (line
== 0 && subfile
->line_vector
->nitems
> 0)
715 e
= subfile
->line_vector
->item
+ subfile
->line_vector
->nitems
- 1;
716 while (subfile
->line_vector
->nitems
> 0 && e
->pc
== pc
)
719 subfile
->line_vector
->nitems
--;
723 e
= subfile
->line_vector
->item
+ subfile
->line_vector
->nitems
++;
728 /* Needed in order to sort line tables from IBM xcoff files. Sigh! */
731 lte_is_less_than (const linetable_entry
&ln1
, const linetable_entry
&ln2
)
733 /* Note: this code does not assume that CORE_ADDRs can fit in ints.
734 Please keep it that way. */
741 /* If pc equal, sort by line. I'm not sure whether this is optimum
742 behavior (see comment at struct linetable in symtab.h). */
743 return ln1
.line
< ln2
.line
;
746 /* Subroutine of end_symtab to simplify it. Look for a subfile that
747 matches the main source file's basename. If there is only one, and
748 if the main source file doesn't have any symbol or line number
749 information, then copy this file's symtab and line_vector to the
750 main source file's subfile and discard the other subfile. This can
751 happen because of a compiler bug or from the user playing games
752 with #line or from things like a distributed build system that
753 manipulates the debug info. This can also happen from an innocent
754 symlink in the paths, we don't canonicalize paths here. */
757 buildsym_compunit::watch_main_source_file_lossage ()
759 struct subfile
*mainsub
, *subfile
;
761 /* Get the main source file. */
762 mainsub
= m_main_subfile
;
764 /* If the main source file doesn't have any line number or symbol
765 info, look for an alias in another subfile. */
767 if (mainsub
->line_vector
== NULL
768 && mainsub
->symtab
== NULL
)
770 const char *mainbase
= lbasename (mainsub
->name
);
772 struct subfile
*prevsub
;
773 struct subfile
*mainsub_alias
= NULL
;
774 struct subfile
*prev_mainsub_alias
= NULL
;
777 for (subfile
= m_subfiles
;
779 subfile
= subfile
->next
)
781 if (subfile
== mainsub
)
783 if (filename_cmp (lbasename (subfile
->name
), mainbase
) == 0)
786 mainsub_alias
= subfile
;
787 prev_mainsub_alias
= prevsub
;
794 gdb_assert (mainsub_alias
!= NULL
&& mainsub_alias
!= mainsub
);
796 /* Found a match for the main source file.
797 Copy its line_vector and symtab to the main subfile
798 and then discard it. */
800 mainsub
->line_vector
= mainsub_alias
->line_vector
;
801 mainsub
->line_vector_length
= mainsub_alias
->line_vector_length
;
802 mainsub
->symtab
= mainsub_alias
->symtab
;
804 if (prev_mainsub_alias
== NULL
)
805 m_subfiles
= mainsub_alias
->next
;
807 prev_mainsub_alias
->next
= mainsub_alias
->next
;
808 xfree (mainsub_alias
->name
);
809 xfree (mainsub_alias
);
814 /* Implementation of the first part of end_symtab. It allows modifying
815 STATIC_BLOCK before it gets finalized by end_symtab_from_static_block.
816 If the returned value is NULL there is no blockvector created for
817 this symtab (you still must call end_symtab_from_static_block).
819 END_ADDR is the same as for end_symtab: the address of the end of the
822 If EXPANDABLE is non-zero the STATIC_BLOCK dictionary is made
825 If REQUIRED is non-zero, then a symtab is created even if it does
826 not contain any symbols. */
829 buildsym_compunit::end_symtab_get_static_block (CORE_ADDR end_addr
,
830 int expandable
, int required
)
832 /* Finish the lexical context of the last function in the file; pop
833 the context stack. */
835 if (!m_context_stack
.empty ())
837 struct context_stack cstk
= pop_context ();
839 /* Make a block for the local symbols within. */
840 finish_block (cstk
.name
, cstk
.old_blocks
, NULL
,
841 cstk
.start_addr
, end_addr
);
843 if (!m_context_stack
.empty ())
845 /* This is said to happen with SCO. The old coffread.c
846 code simply emptied the context stack, so we do the
847 same. FIXME: Find out why it is happening. This is not
848 believed to happen in most cases (even for coffread.c);
849 it used to be an abort(). */
850 complaint (_("Context stack not empty in end_symtab"));
851 m_context_stack
.clear ();
855 /* Reordered executables may have out of order pending blocks; if
856 OBJF_REORDERED is true, then sort the pending blocks. */
858 if ((m_objfile
->flags
& OBJF_REORDERED
) && m_pending_blocks
)
860 struct pending_block
*pb
;
862 std::vector
<block
*> barray
;
864 for (pb
= m_pending_blocks
; pb
!= NULL
; pb
= pb
->next
)
865 barray
.push_back (pb
->block
);
867 /* Sort blocks by start address in descending order. Blocks with the
868 same start address must remain in the original order to preserve
869 inline function caller/callee relationships. */
870 std::stable_sort (barray
.begin (), barray
.end (),
871 [] (const block
*a
, const block
*b
)
873 return BLOCK_START (a
) > BLOCK_START (b
);
877 for (pb
= m_pending_blocks
; pb
!= NULL
; pb
= pb
->next
)
878 pb
->block
= barray
[i
++];
881 /* Cleanup any undefined types that have been left hanging around
882 (this needs to be done before the finish_blocks so that
883 file_symbols is still good).
885 Both cleanup_undefined_stabs_types and finish_global_stabs are stabs
886 specific, but harmless for other symbol readers, since on gdb
887 startup or when finished reading stabs, the state is set so these
888 are no-ops. FIXME: Is this handled right in case of QUIT? Can
889 we make this cleaner? */
891 cleanup_undefined_stabs_types (m_objfile
);
892 finish_global_stabs (m_objfile
);
895 && m_pending_blocks
== NULL
896 && m_file_symbols
== NULL
897 && m_global_symbols
== NULL
898 && !m_have_line_numbers
899 && m_pending_macros
== NULL
900 && m_global_using_directives
== NULL
)
902 /* Ignore symtabs that have no functions with real debugging info. */
907 /* Define the STATIC_BLOCK. */
908 return finish_block_internal (NULL
, get_file_symbols (), NULL
, NULL
,
909 m_last_source_start_addr
,
910 end_addr
, 0, expandable
);
914 /* Subroutine of end_symtab_from_static_block to simplify it.
915 Handle the "have blockvector" case.
916 See end_symtab_from_static_block for a description of the arguments. */
918 struct compunit_symtab
*
919 buildsym_compunit::end_symtab_with_blockvector (struct block
*static_block
,
920 int section
, int expandable
)
922 struct compunit_symtab
*cu
= m_compunit_symtab
;
923 struct blockvector
*blockvector
;
924 struct subfile
*subfile
;
927 gdb_assert (static_block
!= NULL
);
928 gdb_assert (m_subfiles
!= NULL
);
930 end_addr
= BLOCK_END (static_block
);
932 /* Create the GLOBAL_BLOCK and build the blockvector. */
933 finish_block_internal (NULL
, get_global_symbols (), NULL
, NULL
,
934 m_last_source_start_addr
, end_addr
,
936 blockvector
= make_blockvector ();
938 /* Read the line table if it has to be read separately.
939 This is only used by xcoffread.c. */
940 if (m_objfile
->sf
->sym_read_linetable
!= NULL
)
941 m_objfile
->sf
->sym_read_linetable (m_objfile
);
943 /* Handle the case where the debug info specifies a different path
944 for the main source file. It can cause us to lose track of its
945 line number information. */
946 watch_main_source_file_lossage ();
948 /* Now create the symtab objects proper, if not already done,
949 one for each subfile. */
951 for (subfile
= m_subfiles
;
953 subfile
= subfile
->next
)
955 int linetablesize
= 0;
957 if (subfile
->line_vector
)
959 linetablesize
= sizeof (struct linetable
) +
960 subfile
->line_vector
->nitems
* sizeof (struct linetable_entry
);
962 /* Like the pending blocks, the line table may be
963 scrambled in reordered executables. Sort it if
964 OBJF_REORDERED is true. */
965 if (m_objfile
->flags
& OBJF_REORDERED
)
966 std::sort (subfile
->line_vector
->item
,
967 subfile
->line_vector
->item
968 + subfile
->line_vector
->nitems
,
972 /* Allocate a symbol table if necessary. */
973 if (subfile
->symtab
== NULL
)
974 subfile
->symtab
= allocate_symtab (cu
, subfile
->name
);
975 struct symtab
*symtab
= subfile
->symtab
;
977 /* Fill in its components. */
979 if (subfile
->line_vector
)
981 /* Reallocate the line table on the symbol obstack. */
982 SYMTAB_LINETABLE (symtab
) = (struct linetable
*)
983 obstack_alloc (&m_objfile
->objfile_obstack
, linetablesize
);
984 memcpy (SYMTAB_LINETABLE (symtab
), subfile
->line_vector
,
989 SYMTAB_LINETABLE (symtab
) = NULL
;
992 /* Use whatever language we have been using for this
993 subfile, not the one that was deduced in allocate_symtab
994 from the filename. We already did our own deducing when
995 we created the subfile, and we may have altered our
996 opinion of what language it is from things we found in
998 symtab
->language
= subfile
->language
;
1001 /* Make sure the symtab of main_subfile is the first in its list. */
1003 struct symtab
*main_symtab
, *prev_symtab
;
1005 main_symtab
= m_main_subfile
->symtab
;
1007 for (symtab
*symtab
: compunit_filetabs (cu
))
1009 if (symtab
== main_symtab
)
1011 if (prev_symtab
!= NULL
)
1013 prev_symtab
->next
= main_symtab
->next
;
1014 main_symtab
->next
= COMPUNIT_FILETABS (cu
);
1015 COMPUNIT_FILETABS (cu
) = main_symtab
;
1019 prev_symtab
= symtab
;
1021 gdb_assert (main_symtab
== COMPUNIT_FILETABS (cu
));
1024 /* Fill out the compunit symtab. */
1026 if (m_comp_dir
!= NULL
)
1028 /* Reallocate the dirname on the symbol obstack. */
1029 const char *comp_dir
= m_comp_dir
.get ();
1030 COMPUNIT_DIRNAME (cu
) = obstack_strdup (&m_objfile
->objfile_obstack
,
1034 /* Save the debug format string (if any) in the symtab. */
1035 COMPUNIT_DEBUGFORMAT (cu
) = m_debugformat
;
1037 /* Similarly for the producer. */
1038 COMPUNIT_PRODUCER (cu
) = m_producer
;
1040 COMPUNIT_BLOCKVECTOR (cu
) = blockvector
;
1042 struct block
*b
= BLOCKVECTOR_BLOCK (blockvector
, GLOBAL_BLOCK
);
1044 set_block_compunit_symtab (b
, cu
);
1047 COMPUNIT_BLOCK_LINE_SECTION (cu
) = section
;
1049 COMPUNIT_MACRO_TABLE (cu
) = release_macros ();
1051 /* Default any symbols without a specified symtab to the primary symtab. */
1055 /* The main source file's symtab. */
1056 struct symtab
*symtab
= COMPUNIT_FILETABS (cu
);
1058 for (block_i
= 0; block_i
< BLOCKVECTOR_NBLOCKS (blockvector
); block_i
++)
1060 struct block
*block
= BLOCKVECTOR_BLOCK (blockvector
, block_i
);
1062 struct mdict_iterator miter
;
1064 /* Inlined functions may have symbols not in the global or
1065 static symbol lists. */
1066 if (BLOCK_FUNCTION (block
) != NULL
)
1067 if (symbol_symtab (BLOCK_FUNCTION (block
)) == NULL
)
1068 symbol_set_symtab (BLOCK_FUNCTION (block
), symtab
);
1070 /* Note that we only want to fix up symbols from the local
1071 blocks, not blocks coming from included symtabs. That is why
1072 we use ALL_DICT_SYMBOLS here and not ALL_BLOCK_SYMBOLS. */
1073 ALL_DICT_SYMBOLS (BLOCK_MULTIDICT (block
), miter
, sym
)
1074 if (symbol_symtab (sym
) == NULL
)
1075 symbol_set_symtab (sym
, symtab
);
1079 add_compunit_symtab_to_objfile (cu
);
1084 /* Implementation of the second part of end_symtab. Pass STATIC_BLOCK
1085 as value returned by end_symtab_get_static_block.
1087 SECTION is the same as for end_symtab: the section number
1088 (in objfile->section_offsets) of the blockvector and linetable.
1090 If EXPANDABLE is non-zero the GLOBAL_BLOCK dictionary is made
1093 struct compunit_symtab
*
1094 buildsym_compunit::end_symtab_from_static_block (struct block
*static_block
,
1095 int section
, int expandable
)
1097 struct compunit_symtab
*cu
;
1099 if (static_block
== NULL
)
1101 /* Handle the "no blockvector" case.
1102 When this happens there is nothing to record, so there's nothing
1103 to do: memory will be freed up later.
1105 Note: We won't be adding a compunit to the objfile's list of
1106 compunits, so there's nothing to unchain. However, since each symtab
1107 is added to the objfile's obstack we can't free that space.
1108 We could do better, but this is believed to be a sufficiently rare
1113 cu
= end_symtab_with_blockvector (static_block
, section
, expandable
);
1118 /* Finish the symbol definitions for one main source file, close off
1119 all the lexical contexts for that file (creating struct block's for
1120 them), then make the struct symtab for that file and put it in the
1123 END_ADDR is the address of the end of the file's text. SECTION is
1124 the section number (in objfile->section_offsets) of the blockvector
1127 Note that it is possible for end_symtab() to return NULL. In
1128 particular, for the DWARF case at least, it will return NULL when
1129 it finds a compilation unit that has exactly one DIE, a
1130 TAG_compile_unit DIE. This can happen when we link in an object
1131 file that was compiled from an empty source file. Returning NULL
1132 is probably not the correct thing to do, because then gdb will
1133 never know about this empty file (FIXME).
1135 If you need to modify STATIC_BLOCK before it is finalized you should
1136 call end_symtab_get_static_block and end_symtab_from_static_block
1139 struct compunit_symtab
*
1140 buildsym_compunit::end_symtab (CORE_ADDR end_addr
, int section
)
1142 struct block
*static_block
;
1144 static_block
= end_symtab_get_static_block (end_addr
, 0, 0);
1145 return end_symtab_from_static_block (static_block
, section
, 0);
1148 /* Same as end_symtab except create a symtab that can be later added to. */
1150 struct compunit_symtab
*
1151 buildsym_compunit::end_expandable_symtab (CORE_ADDR end_addr
, int section
)
1153 struct block
*static_block
;
1155 static_block
= end_symtab_get_static_block (end_addr
, 1, 0);
1156 return end_symtab_from_static_block (static_block
, section
, 1);
1159 /* Subroutine of augment_type_symtab to simplify it.
1160 Attach the main source file's symtab to all symbols in PENDING_LIST that
1164 set_missing_symtab (struct pending
*pending_list
,
1165 struct compunit_symtab
*cu
)
1167 struct pending
*pending
;
1170 for (pending
= pending_list
; pending
!= NULL
; pending
= pending
->next
)
1172 for (i
= 0; i
< pending
->nsyms
; ++i
)
1174 if (symbol_symtab (pending
->symbol
[i
]) == NULL
)
1175 symbol_set_symtab (pending
->symbol
[i
], COMPUNIT_FILETABS (cu
));
1180 /* Same as end_symtab, but for the case where we're adding more symbols
1181 to an existing symtab that is known to contain only type information.
1182 This is the case for DWARF4 Type Units. */
1185 buildsym_compunit::augment_type_symtab ()
1187 struct compunit_symtab
*cust
= m_compunit_symtab
;
1188 const struct blockvector
*blockvector
= COMPUNIT_BLOCKVECTOR (cust
);
1190 if (!m_context_stack
.empty ())
1191 complaint (_("Context stack not empty in augment_type_symtab"));
1192 if (m_pending_blocks
!= NULL
)
1193 complaint (_("Blocks in a type symtab"));
1194 if (m_pending_macros
!= NULL
)
1195 complaint (_("Macro in a type symtab"));
1196 if (m_have_line_numbers
)
1197 complaint (_("Line numbers recorded in a type symtab"));
1199 if (m_file_symbols
!= NULL
)
1201 struct block
*block
= BLOCKVECTOR_BLOCK (blockvector
, STATIC_BLOCK
);
1203 /* First mark any symbols without a specified symtab as belonging
1204 to the primary symtab. */
1205 set_missing_symtab (m_file_symbols
, cust
);
1207 mdict_add_pending (BLOCK_MULTIDICT (block
), m_file_symbols
);
1210 if (m_global_symbols
!= NULL
)
1212 struct block
*block
= BLOCKVECTOR_BLOCK (blockvector
, GLOBAL_BLOCK
);
1214 /* First mark any symbols without a specified symtab as belonging
1215 to the primary symtab. */
1216 set_missing_symtab (m_global_symbols
, cust
);
1218 mdict_add_pending (BLOCK_MULTIDICT (block
),
1223 /* Push a context block. Args are an identifying nesting level
1224 (checkable when you pop it), and the starting PC address of this
1227 struct context_stack
*
1228 buildsym_compunit::push_context (int desc
, CORE_ADDR valu
)
1230 m_context_stack
.emplace_back ();
1231 struct context_stack
*newobj
= &m_context_stack
.back ();
1233 newobj
->depth
= desc
;
1234 newobj
->locals
= m_local_symbols
;
1235 newobj
->old_blocks
= m_pending_blocks
;
1236 newobj
->start_addr
= valu
;
1237 newobj
->local_using_directives
= m_local_using_directives
;
1238 newobj
->name
= NULL
;
1240 m_local_symbols
= NULL
;
1241 m_local_using_directives
= NULL
;
1246 /* Pop a context block. Returns the address of the context block just
1249 struct context_stack
1250 buildsym_compunit::pop_context ()
1252 gdb_assert (!m_context_stack
.empty ());
1253 struct context_stack result
= m_context_stack
.back ();
1254 m_context_stack
.pop_back ();