1 /* Support routines for building symbol tables in GDB's internal format.
2 Copyright (C) 1986-2022 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 "gdbsupport/gdb_obstack.h"
23 #include "gdbsupport/pathstuff.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 buildsym_compunit::buildsym_compunit (struct objfile
*objfile_
,
55 const char *comp_dir_
,
56 enum language language_
,
58 : m_objfile (objfile_
),
59 m_last_source_file (name
== nullptr ? nullptr : xstrdup (name
)),
60 m_comp_dir (comp_dir_
== nullptr ? "" : comp_dir_
),
61 m_language (language_
),
62 m_last_source_start_addr (last_addr
)
64 /* Allocate the compunit symtab now. The caller needs it to allocate
65 non-primary symtabs. It is also needed by get_macro_table. */
66 m_compunit_symtab
= allocate_compunit_symtab (m_objfile
, name
);
68 /* Build the subfile for NAME (the main source file) so that we can record
69 a pointer to it for later.
70 IMPORTANT: Do not allocate a struct symtab for NAME here.
71 It can happen that the debug info provides a different path to NAME than
72 DIRNAME,NAME. We cope with this in watch_main_source_file_lossage but
73 that only works if the main_subfile doesn't have a symtab yet. */
75 /* Save this so that we don't have to go looking for it at the end
76 of the subfiles list. */
77 m_main_subfile
= m_current_subfile
;
80 buildsym_compunit::~buildsym_compunit ()
82 struct subfile
*subfile
, *nextsub
;
84 if (m_pending_macros
!= nullptr)
85 free_macro_table (m_pending_macros
);
87 for (subfile
= m_subfiles
;
91 nextsub
= subfile
->next
;
95 struct pending
*next
, *next1
;
97 for (next
= m_file_symbols
; next
!= NULL
; next
= next1
)
100 xfree ((void *) next
);
103 for (next
= m_global_symbols
; next
!= NULL
; next
= next1
)
106 xfree ((void *) next
);
111 buildsym_compunit::get_macro_table ()
113 if (m_pending_macros
== nullptr)
114 m_pending_macros
= new_macro_table (&m_objfile
->per_bfd
->storage_obstack
,
115 &m_objfile
->per_bfd
->string_cache
,
117 return m_pending_macros
;
120 /* Maintain the lists of symbols and blocks. */
122 /* Add a symbol to one of the lists of symbols. */
125 add_symbol_to_list (struct symbol
*symbol
, struct pending
**listhead
)
127 struct pending
*link
;
129 /* If this is an alias for another symbol, don't add it. */
130 if (symbol
->linkage_name () && symbol
->linkage_name ()[0] == '#')
133 /* We keep PENDINGSIZE symbols in each link of the list. If we
134 don't have a link with room in it, add a new link. */
135 if (*listhead
== NULL
|| (*listhead
)->nsyms
== PENDINGSIZE
)
137 link
= XNEW (struct pending
);
138 link
->next
= *listhead
;
143 (*listhead
)->symbol
[(*listhead
)->nsyms
++] = symbol
;
146 /* Find a symbol named NAME on a LIST. NAME need not be
147 '\0'-terminated; LENGTH is the length of the name. */
150 find_symbol_in_list (struct pending
*list
, char *name
, int length
)
157 for (j
= list
->nsyms
; --j
>= 0;)
159 pp
= list
->symbol
[j
]->linkage_name ();
160 if (*pp
== *name
&& strncmp (pp
, name
, length
) == 0
161 && pp
[length
] == '\0')
163 return (list
->symbol
[j
]);
171 /* Record BLOCK on the list of all blocks in the file. Put it after
172 OPBLOCK, or at the beginning if opblock is NULL. This puts the
173 block in the list after all its subblocks. */
176 buildsym_compunit::record_pending_block (struct block
*block
,
177 struct pending_block
*opblock
)
179 struct pending_block
*pblock
;
181 pblock
= XOBNEW (&m_pending_block_obstack
, struct pending_block
);
182 pblock
->block
= block
;
185 pblock
->next
= opblock
->next
;
186 opblock
->next
= pblock
;
190 pblock
->next
= m_pending_blocks
;
191 m_pending_blocks
= pblock
;
195 /* Take one of the lists of symbols and make a block from it. Keep
196 the order the symbols have in the list (reversed from the input
197 file). Put the block on the list of pending blocks. */
200 buildsym_compunit::finish_block_internal
201 (struct symbol
*symbol
,
202 struct pending
**listhead
,
203 struct pending_block
*old_blocks
,
204 const struct dynamic_prop
*static_link
,
205 CORE_ADDR start
, CORE_ADDR end
,
206 int is_global
, int expandable
)
208 struct gdbarch
*gdbarch
= m_objfile
->arch ();
209 struct pending
*next
, *next1
;
211 struct pending_block
*pblock
;
212 struct pending_block
*opblock
;
215 ? allocate_global_block (&m_objfile
->objfile_obstack
)
216 : allocate_block (&m_objfile
->objfile_obstack
));
220 BLOCK_MULTIDICT (block
)
221 = mdict_create_linear (&m_objfile
->objfile_obstack
, *listhead
);
227 BLOCK_MULTIDICT (block
) = mdict_create_hashed_expandable (m_language
);
228 mdict_add_pending (BLOCK_MULTIDICT (block
), *listhead
);
232 BLOCK_MULTIDICT (block
) =
233 mdict_create_hashed (&m_objfile
->objfile_obstack
, *listhead
);
237 BLOCK_START (block
) = start
;
238 BLOCK_END (block
) = end
;
240 /* Put the block in as the value of the symbol that names it. */
244 struct type
*ftype
= symbol
->type ();
245 struct mdict_iterator miter
;
246 symbol
->set_value_block (block
);
247 BLOCK_FUNCTION (block
) = symbol
;
249 if (ftype
->num_fields () <= 0)
251 /* No parameter type information is recorded with the
252 function's type. Set that from the type of the
253 parameter symbols. */
254 int nparams
= 0, iparams
;
257 /* Here we want to directly access the dictionary, because
258 we haven't fully initialized the block yet. */
259 ALL_DICT_SYMBOLS (BLOCK_MULTIDICT (block
), miter
, sym
)
261 if (sym
->is_argument ())
266 ftype
->set_num_fields (nparams
);
269 TYPE_ALLOC (ftype
, nparams
* sizeof (struct field
)));
272 /* Here we want to directly access the dictionary, because
273 we haven't fully initialized the block yet. */
274 ALL_DICT_SYMBOLS (BLOCK_MULTIDICT (block
), miter
, sym
)
276 if (iparams
== nparams
)
279 if (sym
->is_argument ())
281 ftype
->field (iparams
).set_type (sym
->type ());
282 TYPE_FIELD_ARTIFICIAL (ftype
, iparams
) = 0;
291 BLOCK_FUNCTION (block
) = NULL
;
294 if (static_link
!= NULL
)
295 objfile_register_static_link (m_objfile
, block
, static_link
);
297 /* Now free the links of the list, and empty the list. */
299 for (next
= *listhead
; next
; next
= next1
)
306 /* Check to be sure that the blocks have an end address that is
307 greater than starting address. */
309 if (BLOCK_END (block
) < BLOCK_START (block
))
313 complaint (_("block end address less than block "
314 "start address in %s (patched it)"),
315 symbol
->print_name ());
319 complaint (_("block end address %s less than block "
320 "start address %s (patched it)"),
321 paddress (gdbarch
, BLOCK_END (block
)),
322 paddress (gdbarch
, BLOCK_START (block
)));
324 /* Better than nothing. */
325 BLOCK_END (block
) = BLOCK_START (block
);
328 /* Install this block as the superblock of all blocks made since the
329 start of this scope that don't have superblocks yet. */
332 for (pblock
= m_pending_blocks
;
333 pblock
&& pblock
!= old_blocks
;
334 pblock
= pblock
->next
)
336 if (BLOCK_SUPERBLOCK (pblock
->block
) == NULL
)
338 /* Check to be sure the blocks are nested as we receive
339 them. If the compiler/assembler/linker work, this just
340 burns a small amount of time.
342 Skip blocks which correspond to a function; they're not
343 physically nested inside this other blocks, only
345 if (BLOCK_FUNCTION (pblock
->block
) == NULL
346 && (BLOCK_START (pblock
->block
) < BLOCK_START (block
)
347 || BLOCK_END (pblock
->block
) > BLOCK_END (block
)))
351 complaint (_("inner block not inside outer block in %s"),
352 symbol
->print_name ());
356 complaint (_("inner block (%s-%s) not "
357 "inside outer block (%s-%s)"),
358 paddress (gdbarch
, BLOCK_START (pblock
->block
)),
359 paddress (gdbarch
, BLOCK_END (pblock
->block
)),
360 paddress (gdbarch
, BLOCK_START (block
)),
361 paddress (gdbarch
, BLOCK_END (block
)));
363 if (BLOCK_START (pblock
->block
) < BLOCK_START (block
))
364 BLOCK_START (pblock
->block
) = BLOCK_START (block
);
365 if (BLOCK_END (pblock
->block
) > BLOCK_END (block
))
366 BLOCK_END (pblock
->block
) = BLOCK_END (block
);
368 BLOCK_SUPERBLOCK (pblock
->block
) = block
;
373 block_set_using (block
,
375 ? m_global_using_directives
376 : m_local_using_directives
),
377 &m_objfile
->objfile_obstack
);
379 m_global_using_directives
= NULL
;
381 m_local_using_directives
= NULL
;
383 record_pending_block (block
, opblock
);
389 buildsym_compunit::finish_block (struct symbol
*symbol
,
390 struct pending_block
*old_blocks
,
391 const struct dynamic_prop
*static_link
,
392 CORE_ADDR start
, CORE_ADDR end
)
394 return finish_block_internal (symbol
, &m_local_symbols
,
395 old_blocks
, static_link
, start
, end
, 0, 0);
398 /* Record that the range of addresses from START to END_INCLUSIVE
399 (inclusive, like it says) belongs to BLOCK. BLOCK's start and end
400 addresses must be set already. You must apply this function to all
401 BLOCK's children before applying it to BLOCK.
403 If a call to this function complicates the picture beyond that
404 already provided by BLOCK_START and BLOCK_END, then we create an
405 address map for the block. */
407 buildsym_compunit::record_block_range (struct block
*block
,
409 CORE_ADDR end_inclusive
)
411 /* If this is any different from the range recorded in the block's
412 own BLOCK_START and BLOCK_END, then note that the address map has
413 become interesting. Note that even if this block doesn't have
414 any "interesting" ranges, some later block might, so we still
415 need to record this block in the addrmap. */
416 if (start
!= BLOCK_START (block
)
417 || end_inclusive
+ 1 != BLOCK_END (block
))
418 m_pending_addrmap_interesting
= true;
420 if (m_pending_addrmap
== nullptr)
421 m_pending_addrmap
= addrmap_create_mutable (&m_pending_addrmap_obstack
);
423 addrmap_set_empty (m_pending_addrmap
, start
, end_inclusive
, block
);
427 buildsym_compunit::make_blockvector ()
429 struct pending_block
*next
;
430 struct blockvector
*blockvector
;
433 /* Count the length of the list of blocks. */
435 for (next
= m_pending_blocks
, i
= 0; next
; next
= next
->next
, i
++)
439 blockvector
= (struct blockvector
*)
440 obstack_alloc (&m_objfile
->objfile_obstack
,
441 (sizeof (struct blockvector
)
442 + (i
- 1) * sizeof (struct block
*)));
444 /* Copy the blocks into the blockvector. This is done in reverse
445 order, which happens to put the blocks into the proper order
446 (ascending starting address). finish_block has hair to insert
447 each block into the list after its subblocks in order to make
448 sure this is true. */
450 BLOCKVECTOR_NBLOCKS (blockvector
) = i
;
451 for (next
= m_pending_blocks
; next
; next
= next
->next
)
453 BLOCKVECTOR_BLOCK (blockvector
, --i
) = next
->block
;
456 free_pending_blocks ();
458 /* If we needed an address map for this symtab, record it in the
460 if (m_pending_addrmap
!= nullptr && m_pending_addrmap_interesting
)
461 BLOCKVECTOR_MAP (blockvector
)
462 = addrmap_create_fixed (m_pending_addrmap
, &m_objfile
->objfile_obstack
);
464 BLOCKVECTOR_MAP (blockvector
) = 0;
466 /* Some compilers output blocks in the wrong order, but we depend on
467 their being in the right order so we can binary search. Check the
468 order and moan about it.
469 Note: Remember that the first two blocks are the global and static
470 blocks. We could special case that fact and begin checking at block 2.
471 To avoid making that assumption we do not. */
472 if (BLOCKVECTOR_NBLOCKS (blockvector
) > 1)
474 for (i
= 1; i
< BLOCKVECTOR_NBLOCKS (blockvector
); i
++)
476 if (BLOCK_START (BLOCKVECTOR_BLOCK (blockvector
, i
- 1))
477 > BLOCK_START (BLOCKVECTOR_BLOCK (blockvector
, i
)))
480 = BLOCK_START (BLOCKVECTOR_BLOCK (blockvector
, i
));
482 complaint (_("block at %s out of order"),
483 hex_string ((LONGEST
) start
));
488 return (blockvector
);
491 /* Start recording information about source code that came from an
492 included (or otherwise merged-in) source file with a different
493 name. NAME is the name of the file (cannot be NULL). */
496 buildsym_compunit::start_subfile (const char *name
)
498 /* See if this subfile is already registered. */
500 for (subfile
*subfile
= m_subfiles
; subfile
; subfile
= subfile
->next
)
502 std::string subfile_name_holder
;
503 const char *subfile_name
;
505 /* If NAME is an absolute path, and this subfile is not, then
506 attempt to create an absolute path to compare. */
507 if (IS_ABSOLUTE_PATH (name
)
508 && !IS_ABSOLUTE_PATH (subfile
->name
)
509 && !m_comp_dir
.empty ())
511 subfile_name_holder
= path_join (m_comp_dir
.c_str (),
512 subfile
->name
.c_str ());
513 subfile_name
= subfile_name_holder
.c_str ();
516 subfile_name
= subfile
->name
.c_str ();
518 if (FILENAME_CMP (subfile_name
, name
) == 0)
520 m_current_subfile
= subfile
;
525 /* This subfile is not known. Add an entry for it. */
527 subfile_up
subfile (new struct subfile
);
528 subfile
->name
= name
;
530 m_current_subfile
= subfile
.get ();
532 /* Default the source language to whatever can be deduced from the
533 filename. If nothing can be deduced (such as for a C/C++ include
534 file with a ".h" extension), then inherit whatever language the
535 previous subfile had. This kludgery is necessary because there
536 is no standard way in some object formats to record the source
537 language. Also, when symtabs are allocated we try to deduce a
538 language then as well, but it is too late for us to use that
539 information while reading symbols, since symtabs aren't allocated
540 until after all the symbols have been processed for a given
543 subfile
->language
= deduce_language_from_filename (subfile
->name
.c_str ());
544 if (subfile
->language
== language_unknown
&& m_subfiles
!= nullptr)
545 subfile
->language
= m_subfiles
->language
;
547 /* If the filename of this subfile ends in .C, then change the
548 language of any pending subfiles from C to C++. We also accept
549 any other C++ suffixes accepted by deduce_language_from_filename. */
550 /* Likewise for f2c. */
552 if (!subfile
->name
.empty ())
555 language sublang
= deduce_language_from_filename (subfile
->name
.c_str ());
557 if (sublang
== language_cplus
|| sublang
== language_fortran
)
558 for (s
= m_subfiles
; s
!= NULL
; s
= s
->next
)
559 if (s
->language
== language_c
)
560 s
->language
= sublang
;
563 /* And patch up this file if necessary. */
564 if (subfile
->language
== language_c
565 && m_subfiles
!= nullptr
566 && (m_subfiles
->language
== language_cplus
567 || m_subfiles
->language
== language_fortran
))
568 subfile
->language
= m_subfiles
->language
;
570 /* Link this subfile at the front of the subfile list. */
571 subfile
->next
= m_subfiles
;
572 m_subfiles
= subfile
.release ();
575 /* For stabs readers, the first N_SO symbol is assumed to be the
576 source file name, and the subfile struct is initialized using that
577 assumption. If another N_SO symbol is later seen, immediately
578 following the first one, then the first one is assumed to be the
579 directory name and the second one is really the source file name.
581 So we have to patch up the subfile struct by moving the old name
582 value to dirname and remembering the new name. Some sanity
583 checking is performed to ensure that the state of the subfile
584 struct is reasonable and that the old name we are assuming to be a
585 directory name actually is (by checking for a trailing '/'). */
588 buildsym_compunit::patch_subfile_names (struct subfile
*subfile
,
592 && m_comp_dir
.empty ()
593 && !subfile
->name
.empty ()
594 && IS_DIR_SEPARATOR (subfile
->name
.back ()))
596 m_comp_dir
= std::move (subfile
->name
);
597 subfile
->name
= name
;
598 set_last_source_file (name
);
600 /* Default the source language to whatever can be deduced from
601 the filename. If nothing can be deduced (such as for a C/C++
602 include file with a ".h" extension), then inherit whatever
603 language the previous subfile had. This kludgery is
604 necessary because there is no standard way in some object
605 formats to record the source language. Also, when symtabs
606 are allocated we try to deduce a language then as well, but
607 it is too late for us to use that information while reading
608 symbols, since symtabs aren't allocated until after all the
609 symbols have been processed for a given source file. */
612 = deduce_language_from_filename (subfile
->name
.c_str ());
613 if (subfile
->language
== language_unknown
614 && subfile
->next
!= NULL
)
616 subfile
->language
= subfile
->next
->language
;
621 /* Handle the N_BINCL and N_EINCL symbol types that act like N_SOL for
622 switching source files (different subfiles, as we call them) within
623 one object file, but using a stack rather than in an arbitrary
627 buildsym_compunit::push_subfile ()
629 gdb_assert (m_current_subfile
!= NULL
);
630 gdb_assert (!m_current_subfile
->name
.empty ());
631 m_subfile_stack
.push_back (m_current_subfile
->name
.c_str ());
635 buildsym_compunit::pop_subfile ()
637 gdb_assert (!m_subfile_stack
.empty ());
638 const char *name
= m_subfile_stack
.back ();
639 m_subfile_stack
.pop_back ();
643 /* Add a linetable entry for line number LINE and address PC to the
644 line vector for SUBFILE. */
647 buildsym_compunit::record_line (struct subfile
*subfile
, int line
,
648 CORE_ADDR pc
, linetable_entry_flags flags
)
650 m_have_line_numbers
= true;
652 /* Normally, we treat lines as unsorted. But the end of sequence
653 marker is special. We sort line markers at the same PC by line
654 number, so end of sequence markers (which have line == 0) appear
655 first. This is right if the marker ends the previous function,
656 and there is no padding before the next function. But it is
657 wrong if the previous line was empty and we are now marking a
658 switch to a different subfile. We must leave the end of sequence
659 marker at the end of this group of lines, not sort the empty line
660 to after the marker. The easiest way to accomplish this is to
661 delete any empty lines from our table, if they are followed by
662 end of sequence markers. All we lose is the ability to set
663 breakpoints at some lines which contain no instructions
667 gdb::optional
<int> last_line
;
669 while (!subfile
->line_vector_entries
.empty ())
671 linetable_entry
*last
= &subfile
->line_vector_entries
.back ();
672 last_line
= last
->line
;
677 subfile
->line_vector_entries
.pop_back ();
680 /* Ignore an end-of-sequence marker marking an empty sequence. */
681 if (!last_line
.has_value () || *last_line
== 0)
685 subfile
->line_vector_entries
.emplace_back ();
686 linetable_entry
&e
= subfile
->line_vector_entries
.back ();
688 e
.is_stmt
= (flags
& LEF_IS_STMT
) != 0;
690 e
.prologue_end
= (flags
& LEF_PROLOGUE_END
) != 0;
694 /* Subroutine of end_compunit_symtab to simplify it. Look for a subfile that
695 matches the main source file's basename. If there is only one, and
696 if the main source file doesn't have any symbol or line number
697 information, then copy this file's symtab and line_vector to the
698 main source file's subfile and discard the other subfile. This can
699 happen because of a compiler bug or from the user playing games
700 with #line or from things like a distributed build system that
701 manipulates the debug info. This can also happen from an innocent
702 symlink in the paths, we don't canonicalize paths here. */
705 buildsym_compunit::watch_main_source_file_lossage ()
707 struct subfile
*mainsub
, *subfile
;
709 /* Get the main source file. */
710 mainsub
= m_main_subfile
;
712 /* If the main source file doesn't have any line number or symbol
713 info, look for an alias in another subfile. */
715 if (mainsub
->line_vector_entries
.empty ()
716 && mainsub
->symtab
== NULL
)
718 const char *mainbase
= lbasename (mainsub
->name
.c_str ());
720 struct subfile
*prevsub
;
721 struct subfile
*mainsub_alias
= NULL
;
722 struct subfile
*prev_mainsub_alias
= NULL
;
725 for (subfile
= m_subfiles
;
727 subfile
= subfile
->next
)
729 if (subfile
== mainsub
)
731 if (filename_cmp (lbasename (subfile
->name
.c_str ()), mainbase
) == 0)
734 mainsub_alias
= subfile
;
735 prev_mainsub_alias
= prevsub
;
742 gdb_assert (mainsub_alias
!= NULL
&& mainsub_alias
!= mainsub
);
744 /* Found a match for the main source file.
745 Copy its line_vector and symtab to the main subfile
746 and then discard it. */
748 mainsub
->line_vector_entries
749 = std::move (mainsub_alias
->line_vector_entries
);
750 mainsub
->symtab
= mainsub_alias
->symtab
;
752 if (prev_mainsub_alias
== NULL
)
753 m_subfiles
= mainsub_alias
->next
;
755 prev_mainsub_alias
->next
= mainsub_alias
->next
;
757 delete mainsub_alias
;
762 /* Implementation of the first part of end_compunit_symtab. It allows modifying
763 STATIC_BLOCK before it gets finalized by
764 end_compunit_symtab_from_static_block. If the returned value is NULL there
765 is no blockvector created for this symtab (you still must call
766 end_compunit_symtab_from_static_block).
768 END_ADDR is the same as for end_compunit_symtab: the address of the end of
771 If EXPANDABLE is non-zero the STATIC_BLOCK dictionary is made
774 If REQUIRED is non-zero, then a symtab is created even if it does
775 not contain any symbols. */
778 buildsym_compunit::end_compunit_symtab_get_static_block (CORE_ADDR end_addr
,
782 /* Finish the lexical context of the last function in the file; pop
783 the context stack. */
785 if (!m_context_stack
.empty ())
787 struct context_stack cstk
= pop_context ();
789 /* Make a block for the local symbols within. */
790 finish_block (cstk
.name
, cstk
.old_blocks
, NULL
,
791 cstk
.start_addr
, end_addr
);
793 if (!m_context_stack
.empty ())
795 /* This is said to happen with SCO. The old coffread.c
796 code simply emptied the context stack, so we do the
797 same. FIXME: Find out why it is happening. This is not
798 believed to happen in most cases (even for coffread.c);
799 it used to be an abort(). */
800 complaint (_("Context stack not empty in end_compunit_symtab"));
801 m_context_stack
.clear ();
805 /* Reordered executables may have out of order pending blocks; if
806 OBJF_REORDERED is true, then sort the pending blocks. */
808 if ((m_objfile
->flags
& OBJF_REORDERED
) && m_pending_blocks
)
810 struct pending_block
*pb
;
812 std::vector
<block
*> barray
;
814 for (pb
= m_pending_blocks
; pb
!= NULL
; pb
= pb
->next
)
815 barray
.push_back (pb
->block
);
817 /* Sort blocks by start address in descending order. Blocks with the
818 same start address must remain in the original order to preserve
819 inline function caller/callee relationships. */
820 std::stable_sort (barray
.begin (), barray
.end (),
821 [] (const block
*a
, const block
*b
)
823 return BLOCK_START (a
) > BLOCK_START (b
);
827 for (pb
= m_pending_blocks
; pb
!= NULL
; pb
= pb
->next
)
828 pb
->block
= barray
[i
++];
831 /* Cleanup any undefined types that have been left hanging around
832 (this needs to be done before the finish_blocks so that
833 file_symbols is still good).
835 Both cleanup_undefined_stabs_types and finish_global_stabs are stabs
836 specific, but harmless for other symbol readers, since on gdb
837 startup or when finished reading stabs, the state is set so these
838 are no-ops. FIXME: Is this handled right in case of QUIT? Can
839 we make this cleaner? */
841 cleanup_undefined_stabs_types (m_objfile
);
842 finish_global_stabs (m_objfile
);
845 && m_pending_blocks
== NULL
846 && m_file_symbols
== NULL
847 && m_global_symbols
== NULL
848 && !m_have_line_numbers
849 && m_pending_macros
== NULL
850 && m_global_using_directives
== NULL
)
852 /* Ignore symtabs that have no functions with real debugging info. */
857 /* Define the STATIC_BLOCK. */
858 return finish_block_internal (NULL
, get_file_symbols (), NULL
, NULL
,
859 m_last_source_start_addr
,
860 end_addr
, 0, expandable
);
864 /* Subroutine of end_compunit_symtab_from_static_block to simplify it.
865 Handle the "have blockvector" case.
866 See end_compunit_symtab_from_static_block for a description of the
869 struct compunit_symtab
*
870 buildsym_compunit::end_compunit_symtab_with_blockvector
871 (struct block
*static_block
, int section
, int expandable
)
873 struct compunit_symtab
*cu
= m_compunit_symtab
;
874 struct blockvector
*blockvector
;
875 struct subfile
*subfile
;
878 gdb_assert (static_block
!= NULL
);
879 gdb_assert (m_subfiles
!= NULL
);
881 end_addr
= BLOCK_END (static_block
);
883 /* Create the GLOBAL_BLOCK and build the blockvector. */
884 finish_block_internal (NULL
, get_global_symbols (), NULL
, NULL
,
885 m_last_source_start_addr
, end_addr
,
887 blockvector
= make_blockvector ();
889 /* Read the line table if it has to be read separately.
890 This is only used by xcoffread.c. */
891 if (m_objfile
->sf
->sym_read_linetable
!= NULL
)
892 m_objfile
->sf
->sym_read_linetable (m_objfile
);
894 /* Handle the case where the debug info specifies a different path
895 for the main source file. It can cause us to lose track of its
896 line number information. */
897 watch_main_source_file_lossage ();
899 /* Now create the symtab objects proper, if not already done,
900 one for each subfile. */
902 for (subfile
= m_subfiles
;
904 subfile
= subfile
->next
)
906 if (!subfile
->line_vector_entries
.empty ())
908 const auto lte_is_less_than
909 = [] (const linetable_entry
&ln1
,
910 const linetable_entry
&ln2
) -> bool
913 && ((ln1
.line
== 0) != (ln2
.line
== 0)))
914 return ln1
.line
== 0;
916 return (ln1
.pc
< ln2
.pc
);
919 /* Like the pending blocks, the line table may be scrambled in
920 reordered executables. Sort it if OBJF_REORDERED is true. It
921 is important to preserve the order of lines at the same
922 address, as this maintains the inline function caller/callee
923 relationships, this is why std::stable_sort is used. */
924 if (m_objfile
->flags
& OBJF_REORDERED
)
925 std::stable_sort (subfile
->line_vector_entries
.begin (),
926 subfile
->line_vector_entries
.end (),
930 /* Allocate a symbol table if necessary. */
931 if (subfile
->symtab
== NULL
)
932 subfile
->symtab
= allocate_symtab (cu
, subfile
->name
.c_str ());
934 struct symtab
*symtab
= subfile
->symtab
;
936 /* Fill in its components. */
938 if (!subfile
->line_vector_entries
.empty ())
940 /* Reallocate the line table on the objfile obstack. */
941 size_t n_entries
= subfile
->line_vector_entries
.size ();
942 size_t entry_array_size
= n_entries
* sizeof (struct linetable_entry
);
943 int linetablesize
= sizeof (struct linetable
) + entry_array_size
;
945 symtab
->set_linetable
946 (XOBNEWVAR (&m_objfile
->objfile_obstack
, struct linetable
,
949 symtab
->linetable ()->nitems
= n_entries
;
950 memcpy (symtab
->linetable ()->item
,
951 subfile
->line_vector_entries
.data (), entry_array_size
);
954 symtab
->set_linetable (nullptr);
956 /* Use whatever language we have been using for this
957 subfile, not the one that was deduced in allocate_symtab
958 from the filename. We already did our own deducing when
959 we created the subfile, and we may have altered our
960 opinion of what language it is from things we found in
962 symtab
->set_language (subfile
->language
);
965 /* Make sure the filetab of main_subfile is the primary filetab of the CU. */
966 cu
->set_primary_filetab (m_main_subfile
->symtab
);
968 /* Fill out the compunit symtab. */
970 if (!m_comp_dir
.empty ())
972 /* Reallocate the dirname on the symbol obstack. */
973 cu
->set_dirname (obstack_strdup (&m_objfile
->objfile_obstack
,
974 m_comp_dir
.c_str ()));
977 /* Save the debug format string (if any) in the symtab. */
978 cu
->set_debugformat (m_debugformat
);
980 /* Similarly for the producer. */
981 cu
->set_producer (m_producer
);
983 cu
->set_blockvector (blockvector
);
985 struct block
*b
= BLOCKVECTOR_BLOCK (blockvector
, GLOBAL_BLOCK
);
987 set_block_compunit_symtab (b
, cu
);
990 cu
->set_block_line_section (section
);
992 cu
->set_macro_table (release_macros ());
994 /* Default any symbols without a specified symtab to the primary symtab. */
998 /* The main source file's symtab. */
999 struct symtab
*symtab
= cu
->primary_filetab ();
1001 for (block_i
= 0; block_i
< BLOCKVECTOR_NBLOCKS (blockvector
); block_i
++)
1003 struct block
*block
= BLOCKVECTOR_BLOCK (blockvector
, block_i
);
1005 struct mdict_iterator miter
;
1007 /* Inlined functions may have symbols not in the global or
1008 static symbol lists. */
1009 if (BLOCK_FUNCTION (block
) != NULL
)
1010 if (BLOCK_FUNCTION (block
)->symtab () == NULL
)
1011 BLOCK_FUNCTION (block
)->set_symtab (symtab
);
1013 /* Note that we only want to fix up symbols from the local
1014 blocks, not blocks coming from included symtabs. That is why
1015 we use ALL_DICT_SYMBOLS here and not ALL_BLOCK_SYMBOLS. */
1016 ALL_DICT_SYMBOLS (BLOCK_MULTIDICT (block
), miter
, sym
)
1017 if (sym
->symtab () == NULL
)
1018 sym
->set_symtab (symtab
);
1022 add_compunit_symtab_to_objfile (cu
);
1027 /* Implementation of the second part of end_compunit_symtab. Pass STATIC_BLOCK
1028 as value returned by end_compunit_symtab_get_static_block.
1030 SECTION is the same as for end_compunit_symtab: the section number
1031 (in objfile->section_offsets) of the blockvector and linetable.
1033 If EXPANDABLE is non-zero the GLOBAL_BLOCK dictionary is made
1036 struct compunit_symtab
*
1037 buildsym_compunit::end_compunit_symtab_from_static_block
1038 (struct block
*static_block
, int section
, int expandable
)
1040 struct compunit_symtab
*cu
;
1042 if (static_block
== NULL
)
1044 /* Handle the "no blockvector" case.
1045 When this happens there is nothing to record, so there's nothing
1046 to do: memory will be freed up later.
1048 Note: We won't be adding a compunit to the objfile's list of
1049 compunits, so there's nothing to unchain. However, since each symtab
1050 is added to the objfile's obstack we can't free that space.
1051 We could do better, but this is believed to be a sufficiently rare
1056 cu
= end_compunit_symtab_with_blockvector (static_block
, section
, expandable
);
1061 /* Finish the symbol definitions for one main source file, close off
1062 all the lexical contexts for that file (creating struct block's for
1063 them), then make the struct symtab for that file and put it in the
1066 END_ADDR is the address of the end of the file's text. SECTION is
1067 the section number (in objfile->section_offsets) of the blockvector
1070 Note that it is possible for end_compunit_symtab() to return NULL. In
1071 particular, for the DWARF case at least, it will return NULL when
1072 it finds a compilation unit that has exactly one DIE, a
1073 TAG_compile_unit DIE. This can happen when we link in an object
1074 file that was compiled from an empty source file. Returning NULL
1075 is probably not the correct thing to do, because then gdb will
1076 never know about this empty file (FIXME).
1078 If you need to modify STATIC_BLOCK before it is finalized you should
1079 call end_compunit_symtab_get_static_block and
1080 end_compunit_symtab_from_static_block yourself. */
1082 struct compunit_symtab
*
1083 buildsym_compunit::end_compunit_symtab (CORE_ADDR end_addr
, int section
)
1085 struct block
*static_block
;
1087 static_block
= end_compunit_symtab_get_static_block (end_addr
, 0, 0);
1088 return end_compunit_symtab_from_static_block (static_block
, section
, 0);
1091 /* Same as end_compunit_symtab except create a symtab that can be later added
1094 struct compunit_symtab
*
1095 buildsym_compunit::end_expandable_symtab (CORE_ADDR end_addr
, int section
)
1097 struct block
*static_block
;
1099 static_block
= end_compunit_symtab_get_static_block (end_addr
, 1, 0);
1100 return end_compunit_symtab_from_static_block (static_block
, section
, 1);
1103 /* Subroutine of augment_type_symtab to simplify it.
1104 Attach the main source file's symtab to all symbols in PENDING_LIST that
1108 set_missing_symtab (struct pending
*pending_list
,
1109 struct compunit_symtab
*cu
)
1111 struct pending
*pending
;
1114 for (pending
= pending_list
; pending
!= NULL
; pending
= pending
->next
)
1116 for (i
= 0; i
< pending
->nsyms
; ++i
)
1118 if (pending
->symbol
[i
]->symtab () == NULL
)
1119 pending
->symbol
[i
]->set_symtab (cu
->primary_filetab ());
1124 /* Same as end_compunit_symtab, but for the case where we're adding more symbols
1125 to an existing symtab that is known to contain only type information.
1126 This is the case for DWARF4 Type Units. */
1129 buildsym_compunit::augment_type_symtab ()
1131 struct compunit_symtab
*cust
= m_compunit_symtab
;
1132 const struct blockvector
*blockvector
= cust
->blockvector ();
1134 if (!m_context_stack
.empty ())
1135 complaint (_("Context stack not empty in augment_type_symtab"));
1136 if (m_pending_blocks
!= NULL
)
1137 complaint (_("Blocks in a type symtab"));
1138 if (m_pending_macros
!= NULL
)
1139 complaint (_("Macro in a type symtab"));
1140 if (m_have_line_numbers
)
1141 complaint (_("Line numbers recorded in a type symtab"));
1143 if (m_file_symbols
!= NULL
)
1145 struct block
*block
= BLOCKVECTOR_BLOCK (blockvector
, STATIC_BLOCK
);
1147 /* First mark any symbols without a specified symtab as belonging
1148 to the primary symtab. */
1149 set_missing_symtab (m_file_symbols
, cust
);
1151 mdict_add_pending (BLOCK_MULTIDICT (block
), m_file_symbols
);
1154 if (m_global_symbols
!= NULL
)
1156 struct block
*block
= BLOCKVECTOR_BLOCK (blockvector
, GLOBAL_BLOCK
);
1158 /* First mark any symbols without a specified symtab as belonging
1159 to the primary symtab. */
1160 set_missing_symtab (m_global_symbols
, cust
);
1162 mdict_add_pending (BLOCK_MULTIDICT (block
),
1167 /* Push a context block. Args are an identifying nesting level
1168 (checkable when you pop it), and the starting PC address of this
1171 struct context_stack
*
1172 buildsym_compunit::push_context (int desc
, CORE_ADDR valu
)
1174 m_context_stack
.emplace_back ();
1175 struct context_stack
*newobj
= &m_context_stack
.back ();
1177 newobj
->depth
= desc
;
1178 newobj
->locals
= m_local_symbols
;
1179 newobj
->old_blocks
= m_pending_blocks
;
1180 newobj
->start_addr
= valu
;
1181 newobj
->local_using_directives
= m_local_using_directives
;
1182 newobj
->name
= NULL
;
1184 m_local_symbols
= NULL
;
1185 m_local_using_directives
= NULL
;
1190 /* Pop a context block. Returns the address of the context block just
1193 struct context_stack
1194 buildsym_compunit::pop_context ()
1196 gdb_assert (!m_context_stack
.empty ());
1197 struct context_stack result
= m_context_stack
.back ();
1198 m_context_stack
.pop_back ();